使用SpringMVC+Spring

  前端提交图片文件到Controller,检查上传图片大小是否符合要求

 直接上代码了

1.校验图片大小

  这里提供出验证的方法,用于在需要校验的地方调用

 1 /**
 2      * 验证图片大小
 3      */
 4     private Map<String,Object> validate(MultipartFile image) {
 5         Map<String,Object> jsonMap = new HashMap<String,Object>();
 6         //360*240 30k (长*宽 最大值)
 7         if (!image.isEmpty()) {
 8             try {
 9                 BufferedImage bi = ImageIO.read(image.getInputStream());
10                 String standard = Constant.THUMBNAIL_SIZE;  //Constant.THUMBNAIL_SIZE=360,240,30
11                 String[] stand = standard.split(",");
12                 if (bi.getWidth() > new Integer(stand[0])) {
13                     jsonMap.put(Constant.ERROR_MSG,"缩略图宽度不能大于"+ stand[0] + "px");
14                 }
15                 if (bi.getHeight() > new Integer(stand[1])) {
16                     jsonMap.put(Constant.ERROR_MSG,"缩略图高度不能大于"+ stand[1] + "px");
17                 }
18                 if (image.getSize() / 1024 > new Integer(stand[2])) {
19                     jsonMap.put(Constant.ERROR_MSG,"缩略图大小不能大于"+ stand[2] + "K");
20                 }
21 
22             } catch (IOException e) {
23                 logger.error("图片验证时出现IOException异常!");
24                 e.printStackTrace();
25             }
26         }
27         return jsonMap;
28     }
校验图片大小的方法

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-05-16
  • 2021-12-03
  • 2021-04-15
猜你喜欢
  • 2021-11-28
  • 2021-04-25
  • 2021-08-20
  • 2021-12-20
  • 2021-10-16
相关资源
相似解决方案