使用Thumbnails压缩图片

maven依赖


<dependency>
   <groupId>net.coobirdgroupId>
   <artifactId>thumbnailatorartifactId>
   <version>0.4.8version>
dependency>
详细代码

前端使用ajaxfileupload上传,Java框架springmvc,接受文件流使用CommonsMultipartFile,

(

CommonsMultipartFile file
)

//控制图片的尺寸

BufferedImage image = Thumbnails.of(file.getInputStream()).size(1080,1920).asBufferedImage();
//BufferedImage 转 InputStream
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(image,"png",os);
ByteArrayInputStream byteArrayInputStream   = new ByteArrayInputStream(os.toByteArray());
 
  
result = oSSFileUtil.uploadFileStream(byteArrayInputStream, uploadPath);
详细参考http://blog.csdn.net/chenleixing/article/details/44685817;
供自己日后参考。


你可能感兴趣的:(Java)