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;
    }
好记性不如烂笔头
原文地址:https://www.cnblogs.com/codehello/p/15351906.html