springboot整合thumbnailator实现图片压缩
springboot整合thumbnailator实现图片压缩
前言
最近由于首页产品列表图片显示太慢,经过研究发现是用户上传的图片太大。
针对这个问题,想到的解决方案是:
1、 产品上传时,限定图片上传大小不超过2m
2、 上传成功后将产品图片进行压缩,但是保留原图片,压缩后的图片名称添加后缀”-thumbnail”
3、 对已经上传的产品图片全部进行压缩
4、 前端只有在点击查看产品大图时显示原图,其他情况均显示缩略图
实现
根据需求,找到的解决方法是使用net.coobird.thumbnailator依赖包,实现图片压缩和将指定目录下的图片全部压缩的功能
引入maven依赖
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.8</version>
</dependency>
具体实现
ImageUtil类
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.name.Rename; import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List; /**
* 图片压缩工具类
*
* @author lnj
* createTime 2018-10-19 15:31
**/
public class ImageUtil { // 图片默认缩放比率
private static final double DEFAULT_SCALE = 0.8d; // 缩略图后缀
private static final String SUFFIX = "-thumbnail"; /**
* 生成缩略图到指定的目录
*
* @param path 目录
* @param files 要生成缩略图的文件列表
* @throws IOException
*/
public static List<String> generateThumbnail2Directory(String path, String... files) throws IOException {
return generateThumbnail2Directory(DEFAULT_SCALE, path, files);
} /**
* 生成缩略图到指定的目录
*
* @param scale 图片缩放率
* @param pathname 缩略图保存目录
* @param files 要生成缩略图的文件列表
* @throws IOException
*/
public static List<String> generateThumbnail2Directory(double scale, String pathname, String... files) throws IOException {
Thumbnails.of(files)
// 图片缩放率,不能和size()一起使用
.scale(scale)
// 缩略图保存目录,该目录需存在,否则报错
.toFiles(new File(pathname), Rename.SUFFIX_HYPHEN_THUMBNAIL);
List<String> list = new ArrayList<>(files.length);
for (String file : files) {
list.add(appendSuffix(file, SUFFIX));
}
return list;
} /**
* 将指定目录下所有图片生成缩略图
*
* @param pathname 文件目录
*/
public static void generateDirectoryThumbnail(String pathname) throws IOException {
generateDirectoryThumbnail(pathname, DEFAULT_SCALE);
} /**
* 将指定目录下所有图片生成缩略图
*
* @param pathname 文件目录
*/
public static void generateDirectoryThumbnail(String pathname, double scale) throws IOException {
File[] files = new File(pathname).listFiles();
compressRecurse(files, pathname);
} /**
* 文件追加后缀
*
* @param fileName 原文件名
* @param suffix 文件后缀
* @return
*/
public static String appendSuffix(String fileName, String suffix) {
String newFileName = ""; int indexOfDot = fileName.lastIndexOf('.'); if (indexOfDot != -1) {
newFileName = fileName.substring(0, indexOfDot);
newFileName += suffix;
newFileName += fileName.substring(indexOfDot);
} else {
newFileName = fileName + suffix;
} return newFileName;
} private static void compressRecurse(File[] files, String pathname) throws IOException {
for (File file : files) {
// 目录
if (file.isDirectory()) {
File[] subFiles = file.listFiles();
compressRecurse(subFiles, pathname + File.separator + file.getName());
} else {
// 文件包含压缩文件后缀或非图片格式,则不再压缩
String extension = getFileExtention(file.getName());
if (!file.getName().contains(SUFFIX) && isImage(extension)) {
generateThumbnail2Directory(pathname, file.getAbsolutePath());
}
}
}
} /**
* 根据文件扩展名判断文件是否图片格式
*
* @param extension 文件扩展名
* @return
*/
public static boolean isImage(String extension) {
String[] imageExtension = new String[]{"jpeg", "jpg", "gif", "bmp", "png"}; for (String e : imageExtension) if (extension.toLowerCase().equals(e)) return true; return false;
} public static String getFileExtention(String fileName) {
String extension = fileName.substring(fileName.lastIndexOf(".") + 1);
return extension;
}
}
测试
import org.junit.Test; import java.io.IOException;
import java.util.List; /**
* @author lnj
* createTime 2018-10-19 15:39
**/
public class ImageUtilTest { /**
* 测试在指定目录下生成缩略图
*/
@Test
public void testGenerateThumbnail2Directory() throws IOException {
String path = "D:\\workspace\\idea\\individual\\springboot-learn\\springboot-thumbnail\\image";
String[] files = new String[]{
"image/1.jpg",
"image/2.jpg"
}; List<String> list = ImageUtil.generateThumbnail2Directory(path, files);
System.out.println(list);
} /**
* 将指定目录下的图片生成缩略图
*/
@Test
public void testGenerateDirectoryThumbnail() throws IOException {
String path = "D:\\workspace\\idea\\individual\\springboot-learn\\springboot-thumbnail\\image";
ImageUtil.generateDirectoryThumbnail(path);
}
}
代码下载地址
https://github.com/linj6/springboot-learn/tree/master/springboot-thumbnail
参考资料
https://juejin.im/post/5b138045f265da6e603934ab#comment
springboot整合thumbnailator实现图片压缩的更多相关文章
- springboot整合ueditor实现图片上传和文件上传功能
springboot整合ueditor实现图片上传和文件上传功能 写在前面: 在阅读本篇之前,请先按照我的这篇随笔完成对ueditor的前期配置工作: springboot+layui 整合百度富文本 ...
- Thumbnailator java图片压缩,加水印,批量生成缩略图
地址:http://code.google.com/p/thumbnailator/ 1.指定大小进行缩放 //size(宽度, 高度) /* * 若图片横比200小,高比300小,不变 * 若图片横 ...
- SpringBoot整合FastDFS实现图片的上传
文件的上传和预览在web开发领域是随处可见,存储的方式有很多,本文采用阿里巴巴余庆大神开发的FastDFS进行文件的存储,FastDFS是一个分布式文件存储系统,可以看我上一篇博文,有安装和配置教程 ...
- Java使用google开源工具Thumbnailator实现图片压缩
<dependency> <groupId>net.coobird</groupId> <artifactId>thumbnailator</ar ...
- 玩转 SpringBoot2.x 之整合 thumbnailator 图片处理
1.序 在实际项目中,有时为了响应速度,难免会对一些高清图片进行一些处理,比如图片压缩之类的,而其中压缩可能就是最为常见的.最近,阿淼就被要求实现这个功能,原因是客户那边嫌速度过慢.借此机会,阿淼今儿 ...
- 四:Java使用google的thumbnailator工具对图片压缩水印等做处理
Thumbnailator是一个非常好的图片开源工具 使用方法: 在pom中加入以下jar包 <!-- 图片缩略图 图片压缩 水印 start--> <dependency>& ...
- 分布式文件系统FastDFS简介、搭建、与SpringBoot整合实现图片上传
之前大学时搭建过一个FastDFS的图片服务器,当时只是抱着好奇的态度搭着玩一下,当时搭建采用了一台虚拟机,tracker和storage服务在一台机器上放着,最近翻之前的博客突然想着在两台机器上搭建 ...
- 很详细的SpringBoot整合UEditor教程
很详细的SpringBoot整合UEditor教程 2017年04月10日 20:27:21 小宝2333 阅读数:21529 版权声明:本文为博主原创文章,未经博主允许不得转载. https: ...
- Java后端实现图片压缩技术
今天来说说图片压缩技术,为什么要使用图片压缩,图片上传不就完事了吗?对的,这在几年前可以这么说,因为几年前还没有现在这么大的并发,也没有现在这么关注性能. 如今手机很多,很多人都是通过手机访问网络或者 ...
随机推荐
- 六:YARN Node Labels
参考:http://dongxicheng.org/mapreduce-nextgen/hadoop-yarn-label-based-scheduling/ 为不同的DATANODE打标签,通过标签 ...
- jsp文件中charset和pageEncoding的区别
jsp文件中charset和pageEncoding的区别: contentType的charset是指服务器发送给客户端时的内容编码,contentType里的charset=utf-8是指示页面 ...
- 2d命令行小游戏源码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 一个例子说明mouseover事件与mouseenter事件的区别
<html> <head> <meta charset="UTF-8"> <title>haha</title> < ...
- PART1 一些想法
其实我一直是一个后知后觉的人,这点也是我过了好久才发现的问题,之所以晚发现自己这个毛病,是因为后知后觉==,这有点像是个悖论或者是笑话,但的确是真实存在于我的身上.其实当初为啥来这个学校选计算机的专业 ...
- 《剑指offer》---寻找反转数组最小值
本文算法使用python3实现 1.题目描述: 把一个数组最开始的若干个元素搬到数组的末尾,我们称之为数组的旋转. 输入一个非递减排序的数组的一个旋转,输出旋转数组的最小元素. 例如数组{3,4, ...
- lintcode-173-链表插入排序
173-链表插入排序 用插入排序对链表排序 样例 Given 1->3->2->0->null, return 0->1->2->3->null 标签 ...
- Kafka Streams演示程序
本文从以下六个方面详细介绍Kafka Streams的演示程序: Step 1: 下载代码 Step 2: 启动kafka服务 Step 3: 准备输入topic并启动Kafka生产者 Step 4: ...
- Spring MVC之@RequestBody@ResponseBody详解
引言: 接上一篇文章讲述处理@RequestMapping的方法参数绑定之后,详细介绍下@RequestBody.@ResponseBody的具体用法和使用时机: 简介: @RequestBody 作 ...
- Android 动画之View动画效果和Activity切换动画效果
View动画效果: 1.>>Tween动画通过对View的内容进行一系列的图形变换(平移.缩放.旋转.透明度变换)实现动画效果,补间动画需要使用<set>节点作为根节点,子节点 ...