<dependency>
<groupId>net.coobirdgroupId>
<artifactId>thumbnailatorartifactId>
<version>0.4.7version>
dependency>
package com.springboot.demo.utils;
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.name.Rename;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.imageio.ImageIO;
public class ImgZipBatchUtil {
private static String absolutePath;
private static Long fileSize;
public ImgZipBatchUtil () {
Prop prop = PropKit.use("config.properties");
String rootPath= prop.get("ImagfileZipRootPath");
Long ImagfileZipSize= prop.getLong("ImagfileZipSize");
absolutePath="D:\\workoffice\\work\\WXWork\\1688851781973394\\Cache\\File\\2021-07\\72015911杜合权";
fileSize=ImagfileZipSize;
}
public void start() {
File file=new File(absolutePath);
Long stat=System.currentTimeMillis();
try {
Thumbnails.of(new File(absolutePath).listFiles()).scale(0.5).outputQuality(0.8f)
.toFiles(Rename.NO_CHANGE);
} catch (IOException e) {
e.printStackTrace();
}
Long end =System.currentTimeMillis()-stat;
System.err.println("耗时:"+end);
}
public void traversalImage(File file) {
File[] files = file.listFiles();
for (File f : files) {
if (f.isDirectory()) {
System.err.println("这是文件夹----" + f.getAbsolutePath()+ "-------------");
traversalImage(f);
} else {
String allPath=f.getAbsolutePath();
log.debug(String.format(" %s;文件大小为:%s B",allPath,f.length()));
zipImg(allPath);
}
}
}
public void zipImg(String absolutePath){
File file=new File(absolutePath);
if(file==null||!file.isFile()){
log.error(absolutePath+"不是一个文件");
return;
}
byte[] bytes=File2byte(file);
try {
log.info("原文件大小:"+bytes.length);
while (bytes.length>fileSize*1024){
System.err.println(bytes.length);
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(bytes.length);
Thumbnails.of(inputStream).scale(0.5f)
.outputQuality(0.8f)
.toOutputStream(outputStream);
bytes=outputStream.toByteArray();
}
log.debug(String.format("压缩后的大小: %s B:",bytes.length));
byteToFile(bytes,absolutePath);
} catch (IOException e) {
log.error(absolutePath);
log.error(e);
e.printStackTrace();
}
}
public static byte[] File2byte(File tradeFile){
byte[] buffer = null;
try
{
FileInputStream fis = new FileInputStream(tradeFile);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int n;
while ((n = fis.read(b)) != -1)
{
bos.write(b, 0, n);
}
fis.close();
bos.close();
buffer = bos.toByteArray();
}catch (FileNotFoundException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
return buffer;
}
public static void byteToFile(byte[] bytes, String path)
{
try
{
File localFile = new File(path);
if (!localFile.exists())
{
localFile.createNewFile();
}
OutputStream os = new FileOutputStream(localFile);
os.write(bytes);
os.close();
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String[] args) {
try {
Thumbnails.of("C:\\Users\\DELL\\Desktop\\0024-产权证-04.JPG")
.scale(0.1f)
.outputQuality(1f).outputFormat("jpg")
.toFile("E:\\ftp\\ftpFile\\3-2-1927");
System.out.println("ok");
}catch (Exception e){
System.err.println("文件没有找到"+e.getClass().getName()+":"+e.getMessage());
}
}
public static String zipImageFile(File oldFile,File newFile, int width, int height,float quality) {
if (oldFile == null) {
return null;
}
try {
Image srcFile = ImageIO.read(oldFile);
int w = srcFile.getWidth(null);
int h = srcFile.getHeight(null);
double bili;
if(width>0){
bili=width/(double)w;
height = (int) (h*bili);
}else{
if(height>0){
bili=height/(double)h;
width = (int) (w*bili);
}
}
String srcImgPath = newFile.getAbsoluteFile().toString();
System.out.println(srcImgPath);
String subfix = "jpg";
subfix = srcImgPath.substring(srcImgPath.lastIndexOf(".")+1,srcImgPath.length());
BufferedImage buffImg = null;
if(subfix.equals("png")){
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}else{
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
}
Graphics2D graphics = buffImg.createGraphics();
graphics.setBackground(new Color(255,255,255));
graphics.setColor(new Color(255,255,255));
graphics.fillRect(0, 0, width, height);
graphics.drawImage(srcFile.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
ImageIO.write(buffImg, subfix, new File(srcImgPath));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return newFile.getAbsolutePath();
}
public static String zipWidthHeightImageFile(File oldFile,File newFile, int width, int height,float quality) {
if (oldFile == null) {
return null;
}
String newImage = null;
try {
Image srcFile = ImageIO.read(oldFile);
String srcImgPath = newFile.getAbsoluteFile().toString();
System.out.println(srcImgPath);
String subfix = "jpg";
subfix = srcImgPath.substring(srcImgPath.lastIndexOf(".")+1,srcImgPath.length());
BufferedImage buffImg = null;
if(subfix.equals("png")){
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
}else{
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
}
Graphics2D graphics = buffImg.createGraphics();
graphics.setBackground(new Color(255,255,255));
graphics.setColor(new Color(255,255,255));
graphics.fillRect(0, 0, width, height);
graphics.drawImage(srcFile.getScaledInstance(width, height, Image.SCALE_SMOOTH), 0, 0, null);
ImageIO.write(buffImg, subfix, new File(srcImgPath));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return newImage;
}
}