MultipartFile類型文件和Base64文件類型判斷、大小判斷和像數驗證


1,MultipartFile類型文件

是否為圖片

                    String fileNameOriginal = file.getOriginalFilename();// 文件原名
                    System.out.println("上傳文件名:"+fileNameOriginal);
                    String mimeType = request.getServletContext().getMimeType(fileNameOriginal);
                    if (mimeType.startsWith("image/")) {
//圖片
}

2,像數和大小

/****
     * 添加上傳圖片文件規則驗證 
     * 1,請上傳像素為1080*1080的圖片或正方形像素圖片, 
     * 2,單張圖片不超過20M
     * 
     * @param file
     * @return
     * @throws IOException 
     */
    public boolean checkImage(MultipartFile file, int size) throws IOException {
        long maxSize = size * 1024 * 1024;
        if (file.getSize() > maxSize) {
            return false;
        }
        BufferedImage bufferedImage = ImageIO.read(file.getInputStream());
            if (bufferedImage != null) {
                Integer width = bufferedImage.getWidth();
                Integer height = bufferedImage.getHeight();

                if (width != height) {
                    return false;
                }
            }
        
        return true;
    }

 2,Base64加密文件上傳驗證大小和像素

 /****
     * 上傳文件是否圖片
     * @param base64Str
     * @return
     */   
    public static boolean checkImageBase64Format(String base64ImgData)  {
        boolean flag=false;
        
        String fileSuffix = "";
          try {
                if (base64ImgData.contains("@@")) {
                    String[] split = base64ImgData.split("@@");
                    base64ImgData = split[1];
                    fileSuffix = split[0];
                }

                if (base64ImgData.contains(",")) {
                    String[] split = base64ImgData.split(",");
                    base64ImgData = split[1];
                    fileSuffix = split[0].substring(split[0].indexOf('/') + 1, split[0].indexOf(';'));
                }
            } catch (Exception e) {
                e.printStackTrace();               
            }
        switch (fileSuffix) {
        case "jpeg": flag=true;break;
        case "png": flag=true;break;
        case "jpg": flag=true;break;        
        default:break;
        }
              
        return flag;
    }
 /****
     *  添加上傳圖片文件規則驗證 
     * 1,請上傳像素為1080*1080的圖片或正方形像素圖片, 
        * 2,單張圖片不超過20M
     * @param file,size
     * @return
     * @throws IOException 
     */    
    public static boolean checkImage(String file,int size)  {
        long maxSize = size * 1024 * 1024;              
           String str=file.substring(22); // 1.需要計算文件流大小,首先把頭部的data:image/png;base64,(注意有逗號)去掉。
           Integer equalIndex= str.indexOf("=");//2.找到等號,把等號也去掉
           if(str.indexOf("=")>0) {
               str=str.substring(0, equalIndex);
           }
           Integer strLength=str.length();//3.原來的字符流大小,單位為字節
           long fileSize =strLength-(strLength/8)*2;//4.計算后得到的文件流大小,單位為字節         
           if (fileSize > maxSize) {
               return false;
           }
           BufferedImage bufImg;
        try {
            /***
             * 驗證前去掉data:image/jpeg;base64,
             */                        
            bufImg = ImageIO.read(new ByteArrayInputStream(Base64.decodeBase64(str.getBytes("utf-8"))));                
            if(bufImg!=null) {
                int height=bufImg.getHeight();
                int width=bufImg.getWidth();            
                if(height!=width) {
                    return false; 
                }                
            }
            
        } catch (IOException e) {
            
            e.printStackTrace();
        }
        
        return true;
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM