安装 imagemagick
缩略图压缩文件jar包 <dependency> <groupId>net.coobirdgroupId> <artifactId>thumbnailatorartifactId> <version>0.4.8version> dependency>
按指定大小把图片进行缩放(会遵循原图高宽比例) //按指定大小把图片进行缩和放(会遵循原图高宽比例) //此处把图片压成400×500的缩略图 Thumbnails.of(fromPic).size(400,500).toFile(toPic);//变为400*300,遵循原图比例缩或放到400*某个高度
按照指定比例进行缩小和放大 //按照比例进行缩小和放大 Thumbnails.of(fromPic).scale(0.2f).toFile(toPic);//按比例缩小 Thumbnails.of(fromPic).scale(2f);//按比例放大
项目需要安装 imagemagick,地址:http://www.imagemagick.org/ 有着非常强大的图片处理,动态生成图片,压缩图片,等比压缩JAVA开发需要 用maven依赖
图片尺寸不变,压缩图片文件大小 //图片尺寸不变,压缩图片文件大小outputQuality实现,参数1为最高质量 Thumbnails.of(fromPic).scale(1f).outputQuality(0.25f).toFile(toPic);
这里代码只使用了 图片尺寸不变,压缩文件大小 源码
public static BaseResult uploadFileAndCreateThumbnail(MultipartFile imageFile,HttpServletRequest request,String uploadPath) { if(imageFile == null ){ return new BaseResult(false, 'imageFile不能为空'); } if (imageFile.getSize() >= 10*1024*1024) { return new BaseResult(false, '文件不能大于10M'); } String uuid = UUID.randomUUID().toString(); String fileDirectory = CommonDateUtils.date2string(new Date(), CommonDateUtils.YYYY_MM_DD); //拼接后台文件名称 String pathName = fileDirectory + File.separator + uuid + '.' + FilenameUtils.getExtension(imageFile.getOriginalFilename()); //构建保存文件路劲 //2016-5-6 yangkang 修改上传路径为服务器上 String realPath = request.getServletContext().getRealPath('uploadPath'); //获取服务器绝对路径 linux 服务器地址 获取当前使用的配置文件配置 //String urlString=PropertiesUtil.getInstance().getSysPro('uploadPath'); //拼接文件路劲 String filePathName = realPath + File.separator + pathName; log.info('图片上传路径:'+filePathName); //判断文件保存是否存在 File file = new File(filePathName); if (file.getParentFile() != null || !file.getParentFile().isDirectory()) { //创建文件 file.getParentFile().mkdirs(); } InputStream inputStream = null; FileOutputStream fileOutputStream = null; try { inputStream = imageFile.getInputStream(); fileOutputStream = new FileOutputStream(file); //写出文件 //2016-05-12 yangkang 改为增加缓存 // IOUtils.copy(inputStream, fileOutputStream); byte[] buffer = new byte[2048]; IOUtils.copyLarge(inputStream, fileOutputStream, buffer); buffer = null; } catch (IOException e) { filePathName = null; return new BaseResult(false, '操作失败', e.getMessage()); } finally { try { if (inputStream != null) { inputStream.close(); } if (fileOutputStream != null) { fileOutputStream.flush(); fileOutputStream.close(); } } catch (IOException e) { filePathName = null; return new BaseResult(false, '操作失败', e.getMessage()); } } // String fileId = FastDFSClient.uploadFile(file, filePathName); //拼接后台文件名称 String thumbnailPathName = fileDirectory + File.separator + uuid + 'small.' + FilenameUtils.getExtension(imageFile.getOriginalFilename()); //added by yangkang 2016-3-30 去掉后缀中包含的.png字符串 if(thumbnailPathName.contains('.png')){ thumbnailPathName = thumbnailPathName.replace('.png', '.jpg'); } long size = imageFile.getSize(); double scale = 1.0d ; if(size >= 200*1024){ if(size > 0){ scale = (200*1024f) / size ; } } //拼接文件路劲 String thumbnailFilePathName = realPath + File.separator + thumbnailPathName; try { //added by chenshun 2016-3-22 注释掉之前长宽的方式,改用大小// Thumbnails.of(filePathName).size(width, height).toFile(thumbnailFilePathName); if(size < 200*1024){ Thumbnails.of(filePathName).scale(1f).outputFormat('jpg').toFile(thumbnailFilePathName); }else{ Thumbnails.of(filePathName).scale(1f).outputQuality(scale).outputFormat('jpg').toFile(thumbnailFilePathName); } } catch (Exception e1) { return new BaseResult(false, '操作失败', e1.getMessage()); } Map map = new HashMap(); //原图地址 map.put('originalUrl', pathName); //缩略图地址 map.put('thumbnailUrl', thumbnailPathName); return new BaseResult(true, '操作成功', map); }
纯手打文字,请点【投票】和【大拇指】以资鼓励;业余个人经验分享,肯定有不足的地方请留言,若觉得有用还可以点击右边的【双箭头】来分享;怕下次出问题之后忘记找不到,可点击【☆】来收藏。若有不足,请在【我有疑问】提问,给出您的方法。点击“关注”。