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后端实现图片压缩技术
今天来说说图片压缩技术,为什么要使用图片压缩,图片上传不就完事了吗?对的,这在几年前可以这么说,因为几年前还没有现在这么大的并发,也没有现在这么关注性能. 如今手机很多,很多人都是通过手机访问网络或者 ...
随机推荐
- 使用清华镜像在python中pip 安装
Anaconda的安装步骤不在本文的讨论中,我们主要是学习一下如何配置conda的镜像,以及一些问题的解决过程 配置镜像 在conda安装好之后,默认的镜像是官方的,由于官网的镜像在境外,我们使用国内 ...
- ffmpeg实现mjpeg摄像头的采集-预览-拍照
摄像头输出是mjpeg格式的,需要实现在线预览功能,然后实现拍照功能 1.可以设置采集图像的分辨率,预览分辨率为640*480,可以自定义 2.ctrl+\ 拍照,ctrl+c 退出 void tes ...
- 11.24Daily Scrum(4)
人员 任务分配完成情况 明天任务分配 王皓南 实现网页上视频浏览的功能.研究相关的代码和功能.1007 实现视频浏览的功能 申开亮 实现网页上视频浏览的功能.研究相关的代码和功能.1008 实现视频浏 ...
- Scala快速入门-基础
HelloWorld 从HelloWorld开始,使用scala IDE编辑器. 新建scala project 新建scala object 编写HelloWorld run as scala ap ...
- 3DMAX2016安装教程【图文】
下载安装包之后,双击setup.exe. 下面是安装图片教程: 点击安装 点击下一步. 如图输入序列号和产品密钥. 填写安装路径,然后下一步. 开始安装,等待. 安装成功.
- Windows2008安装启用无线网卡
昨天给本子换了系统来着,本来想法是好的,想在本子上安装Hyper-v来搭建多平台VPS,这样的话就能玩多个系统了,对于我自己来说对娱乐没啥兴趣,扯多了,正文 笔记本安装什么都很顺利,但是无线网卡把我难 ...
- QT分析之网络编程
原文地址:http://blog.163.com/net_worm/blog/static/127702419201002842553382/ 首先对Windows下的网络编程总结一下: 如果是服务器 ...
- Git 应用补丁报错 “sha1 information is lacking or useless”
因为现场代码在客户局域网内,不能连接到公司网络,所以一般更新的时候都是打补丁, 然后在客户现场应用补丁,但是最近在应用补丁的时候出现了如下问题: ... fatal: sha1 information ...
- 三节点搭建openstack-Mitaka版本
前言: 现在的云计算平台已经非常火,也非常的稳定了.像阿里云平台,百度云平台等等,今天咱们基于openstack来搭建一个云平台 注意: 本次平台搭建为三节点搭建(没有外部存储节点,所有存储为本地存储 ...
- CMD命令提示符
mspaint 画图板 notepad 打开记事本 write 写字板 calc.exe 计算器 control.exe 控制面板 osk 打开屏幕键盘 rononce -p ----15 ...