限制上传图片大小,格式为jpg 或者 png
2012-02-10 09:38
567 查看
在Constants.java中:
public static HashMap<String, String> getPhotoTypeMap() {
HashMap<String, String> resultMap = new HashMap<String, String>();
resultMap.put("jpg", "");
resultMap.put("png", "");
return resultMap;
}
@RequestMapping(value = "/uploadAvatarImage", method = RequestMethod.POST)
public String uploadAvatarImage(Model uiModel, @RequestParam("avatarImageFile") MultipartFile file) {
String originalFileName=file.getOriginalFilename();
if (!"".equals(file.getOriginalFilename()) && file.getSize() <= Constants.SYSTEM_IMAGE_MAX_SIZE) {
String type=originalFileName.substring(originalFileName.lastIndexOf(".")+1);
if (!Constants.getPhotoTypeMap().containsKey(type.toLowerCase())) {
uiModel.addAttribute("validateMessage", "The format of the image is not supported, you can only upload the format of jpg or png.");
return showProfile(uiModel,"Y");
}
}
if (file == null || file.getSize() <= 0) {
uiModel.addAttribute("validateMessage", "The image size can not be zero.");
return showProfile(uiModel,"Y");
}
if (file.getSize() > Constants.SYSTEM_IMAGE_MAX_SIZE) {
uiModel.addAttribute("validateMessage", "The image size can not exceed 1024K.");
return showProfile(uiModel,"Y");
}
try {
UploadFile uploadThumbnailImage = Tools.uploadImageWithThumbnail(file, Constants.UPLOADFILE_HOMEOWNER_AVATAR_PATH, Constants.SYSTEM_THUMBNAIL_IMAGE_WIDTH, Constants.SYSTEM_THUMBNAIL_IMAGE_HEIGHT);
if (uploadThumbnailImage != null) {
User currentUser = getCurrentUser();
uploadThumbnailImage.setUser(currentUser);
uploadThumbnailImage.setCategory(Constants.UPLOADFILE_AVATAR_TYPE);
uploadFileService.saveUploadImage(uploadThumbnailImage);
}
} catch (IOException e) {
e.printStackTrace();
}
showProfile(uiModel,"N");
return "redirect:/homeowner/profile";
}
public static HashMap<String, String> getPhotoTypeMap() {
HashMap<String, String> resultMap = new HashMap<String, String>();
resultMap.put("jpg", "");
resultMap.put("png", "");
return resultMap;
}
@RequestMapping(value = "/uploadAvatarImage", method = RequestMethod.POST)
public String uploadAvatarImage(Model uiModel, @RequestParam("avatarImageFile") MultipartFile file) {
String originalFileName=file.getOriginalFilename();
if (!"".equals(file.getOriginalFilename()) && file.getSize() <= Constants.SYSTEM_IMAGE_MAX_SIZE) {
String type=originalFileName.substring(originalFileName.lastIndexOf(".")+1);
if (!Constants.getPhotoTypeMap().containsKey(type.toLowerCase())) {
uiModel.addAttribute("validateMessage", "The format of the image is not supported, you can only upload the format of jpg or png.");
return showProfile(uiModel,"Y");
}
}
if (file == null || file.getSize() <= 0) {
uiModel.addAttribute("validateMessage", "The image size can not be zero.");
return showProfile(uiModel,"Y");
}
if (file.getSize() > Constants.SYSTEM_IMAGE_MAX_SIZE) {
uiModel.addAttribute("validateMessage", "The image size can not exceed 1024K.");
return showProfile(uiModel,"Y");
}
try {
UploadFile uploadThumbnailImage = Tools.uploadImageWithThumbnail(file, Constants.UPLOADFILE_HOMEOWNER_AVATAR_PATH, Constants.SYSTEM_THUMBNAIL_IMAGE_WIDTH, Constants.SYSTEM_THUMBNAIL_IMAGE_HEIGHT);
if (uploadThumbnailImage != null) {
User currentUser = getCurrentUser();
uploadThumbnailImage.setUser(currentUser);
uploadThumbnailImage.setCategory(Constants.UPLOADFILE_AVATAR_TYPE);
uploadFileService.saveUploadImage(uploadThumbnailImage);
}
} catch (IOException e) {
e.printStackTrace();
}
showProfile(uiModel,"N");
return "redirect:/homeowner/profile";
}
相关文章推荐
- java web - 限制文件的上传类型为图片类型(.gif,.png,.jpg,.jpeg)
- 利用Struts拦截器限制上传图片的格式和大小
- js 限制图片上传的格式与大小
- js上传文件(图片)限制格式及大小
- js 异步上传图片 限制图片的格式大小
- 解决eWebEditor上传图片提示:请选择一个有效的文件,支持的格式有(GIF|JPG|JPEG|BMP|PNG)!
- 在ASP.NET MVC下实现单个图片上传, 客户端服务端双重限制图片大小和格式, 服务端裁剪图片
- JS实现点击某张图片弹出上传图片窗口以及限制图片格式和大小
- JS上传图片前的限制包括(jpg jpg gif及大小高宽)等
- 实名认证,上传图片的问题,只能上传图片格式,并且还有限制大小
- 解决eWebEditor上传图片提示:请选择一个有效的文件,支持的格式有(GIF|JPG|JPEG|BMP|PNG)!
- 上传图片限制大小和格式
- UEditor之——图片上传组件大小4M的限制
- jquery 判断当前上传文件大小限制上传格式 搭配thinkphp实现上传即预览(模拟异步上传)
- 修改KindEditor图片批量上传数量和大小限制
- Js下检查上传图片文件格式和大小的方法,兼容ie和火狐
- 图片上传之前检查大小、尺寸、格式并预览的js代码