阅读背景:

图片上传并压缩

来源:互联网 
public void uploadFile(@RequestParam(value = "file", required = false)MultipartFile[] file,HttpServletRequest request, HttpServletResponse response) throws IOException {	
		//附件上传
		for (MultipartFile multipartFile : file) {
			//获取文件名
			String fileName = multipartFile.getOriginalFilename();
			if(null!=fileName && !"".equals(fileName)){
				try {
					String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();//文件扩展名
					String newfilename = StringUtil.getUUID() + "." + fileExt; //生成uuid文件名
					String savePath = "upload/images/" + newfilename;
					File folder=new File(PropUtils.get("web.root")+"upload/images");
					if(!folder.exists()){
						folder.mkdirs();//如果文件夹不存在 创建文件夹
					}
					//上传文件
//					multipartFile.transferTo(new File(PropUtils.get("web.root") + savePath));
					
					//压缩文件
					ImageHelper.compress(multipartFile.getInputStream(), new File(PropUtils.get("web.root") + savePath), 700);
					
					//数据库保存图片路径信息
				} catch (Exception e) {
					e.printStackTrace();
					return;
				}
			}
		}
	}public void uploadFile(@RequestParam(value = "f



你的当前访问异常,请进行认证后继续阅读剩余内容。

分享到: