struts2 上传 验证图片大小 长宽
2017-08-28 11:42
246 查看
/** * 验证图片大小 限制在一M内 * @param file * @return */ public HashMap<String, Object> checkSize(File file){ HashMap<String, Object> map=new HashMap<String, Object>(); FileInputStream ins; try{ ins = new FileInputStream(file); //判断文件是否超过一M if (ins.available() > 1024 * 1024 *1) { file.delete(); map.put("success", false); map.put("msg", "上传文件不得超过1M"); }else{ map.put("success", true); map.put("msg", ""); } }catch (FileNotFoundException e){ log.error(e.getMessage()); map.put("success", false); map.put("msg", "系统读取图片时出现异常,请联系管理员"); }catch (IOException e){ log.error(e.getMessage()); map.put("success", false); map.put("msg", "系统读取图片时出现异常,请联系管理员"); } return map; } /** * 检测文件长度是否符合 * @param height * @return */ public HashMap<String, Object> checkImgSize(File file,String width,String height){ HashMap<String, Object> map=new HashMap<String, Object>(); Image src; try{ src = javax.imageio.ImageIO.read(file); int w = src.getWidth(null); int h = src.getHeight(null); if(width.equals(w+"") && height.equals(h+"")){ map.put("success", true); map.put("msg", "true"); }else{ map.put("success", false); map.put("msg", "文件上传失败,请上传图片宽:"+width+" 高:"+height); } }catch (IOException e){ log.error(e.getMessage()); e.printStackTrace(); map.put("success", false); map.put("msg", "系统读取图片大小出现异常,请联系管理或调整图片"); } return map; }
相关文章推荐
- 基于jquery实现的上传图片及图片大小验证、图片预览效果代码
- Javascript验证上传图片大小[前台处理]
- jquery实现上传图片及图片大小验证、图片预览效果代码(借荐,谢谢原作者)
- js验证上传图片文件大小,类型等
- Javascript验证上传图片大小[前台处理]
- js 图片上传前大小长宽验证代码
- 图片上传前大小长宽验证
- struts2上传图片超过大小给出错误提示
- 上传图片前判断文件格式与大小验证文件是不是图片
- 使用jQuery实现验证上传图片的格式与大小
- js验证上传图片文件大小,类型等
- Struts2图片文件上传,判断图片格式和图片大小
- struts2上传图片验证格式
- struts2上传图片超过大小给出错误提示
- js验证上传图片大小和格式
- js图片上传验证图片格式和大小尺寸
- javascript 前端验证 file 上传图片 尺寸 大小
- 客户端验证上传图片的大小(兼容IE和火狐)
- 客户端验证要上传图片的宽高大小