js端直接判断即可。
//校驗上傳檔案表單  
    function doCkeck(){  
        var strFile = document.getElementById("fileData").value;  
        if(strFile =="" || strFile == null){  
            alert("請選擇上傳檔案!");  
            return false;  
        }else{  
            //檔案擴展名稱  
            var fileExt = document.getElementById("fileData").value.substr(document.getElementById("fileData").value.lastIndexOf(".")).toLowerCase();  
            var allowExt = ".jpg|.gif|.png|.bmp|.pdf|.doc|.docx|.xls|.xlsx|.msg|.xml|.txt|.mail|.ppt|.zip|";              
            if(allowExt != 0 && allowExt.indexOf(fileExt + "|") == -1){  
                alert("請上傳【.jpg|.gif|.png|.bmp|.pdf|.doc|.docx|.xls|.xlsx|.msg|.xml|.txt|.mail|.ppt|.zip】類型的檔案!");  
                return false;  
            }  
              
        }  
        return true;  
    } 

解决方案 »

  1.   

    这种方法主要是用JavaScript在提交的表单的时候验证,如果是在服务器端呢??
      

  2.   

    /**
     * 校验一个文件是否是图片文件
     * 
     * @param filename
     *            : 文件的全路径
     * @return 如果是图片文件,则返回true,如果不是图片文件则返回false
     */
    public static boolean isPic(String filename) {
    try {
    // 读入文件
    File file = new File(filename);
    Image imageSrc = ImageIO.read(file);
    // 得到源图宽
    imageSrc.getWidth(null);
    return true;
    } catch (IOException e) {
    System.out.println("非图片文件:所传入的文件路径代表的文件不是图片文件,确定!!");
    e.printStackTrace();
    return false;
    }
    }