Thumbnails图片压缩处理工具
1、pom.xml引入
<!--图片压缩工具-->
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.12</version>
</dependency>
2、springMVC 上传的文件太大,压缩处理(示例代码)
@RequestMapping(value = "/upload",method = RequestMethod.POST) public RetMsg upload(@RequestParam(value = "file",required = false) MultipartFile file) throws Exception{ if(file==null || file.getSize()==0){ return RetMsg.failure("上传文件不能为空"); } String orgFileName = file.getOriginalFilename(); String contentType =file.getContentType();//文件的contentType byte[] bytes=null;//字节数据 if(file.getSize()>=1024*1024*10){ //10M return RetMsg.failure("上传文件太大,不得超过10M"); } if(file.getSize()>4*1024*1024){//4M 如果图片大于4M进行压缩处理 InputStream in=file.getInputStream(); ByteArrayOutputStream out =new ByteArrayOutputStream(1024*1024); Thumbnails.of(in).scale(0.5,0.5).outputQuality(1.0).toOutputStream(out);//将图片进行压缩 0.5*0.5 bytes=out.toByteArray(); }else{ bytes=file.getBytes(); } uploadService.uploadFile(bytes); return RetMsg.success() ; }
Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1
请问博主能分享一下RetMsg.java这个类到邮箱2324154234@qq.com吗,谢谢!