吉安感知网项目-后端
xiebin
2026-01-06 d207a86cdf1ab52ef8cb7cd83bad8fceab8038cf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package org.sxkj.resource.util;
 
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.IOUtils;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.web.multipart.MultipartFile;
 
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.channels.Channels;
 
public class ImageCompressorUtil {
 
    /**
     * 压缩图片
     * @param originalImage
     * @param scaleFactor
     * @return
     * @throws IOException
     */
    public static MultipartFile compressImage(MultipartFile originalImage, Double scaleFactor) throws IOException {
        if (null==scaleFactor){
            scaleFactor = 0.6;
        }
        // 读取原始图片
        BufferedImage image = ImageIO.read(originalImage.getInputStream());
        // 设置压缩图片名称
        String originalFilename = "ys" + originalImage.getOriginalFilename();
        // 获取原始文件的宽度和高度
        int width = image.getWidth();
        int height = image.getHeight();
        // 计算压缩后的宽度和高度
        int compressedWidth = (int) (scaleFactor * width);
        int compressedHeight = (int) (scaleFactor * height);
 
        // 创建压缩后的图片
        BufferedImage compressedImage = new BufferedImage(compressedWidth, compressedHeight, BufferedImage.TYPE_INT_RGB);
        compressedImage.getGraphics().drawImage(image.getScaledInstance(compressedWidth, compressedHeight, BufferedImage.SCALE_SMOOTH), 0, 0, null);
 
        // 将压缩后的图片转换为MultipartFile
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ImageIO.write(compressedImage, getFileExtension(originalImage), outputStream);
        ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
        // 写入本地-测试
//        BufferedImage bufferedImage = ImageIO.read(inputStream);
//        ImageIO.write(bufferedImage, "jpg", new File("F:\\out.jpg"));
        // 转换
        MockMultipartFile mockMultipartFile
            = new MockMultipartFile("__init__.py", originalFilename, "application/octet-stream", inputStream);
        // 关闭流
        outputStream.close();
        inputStream.close();
        // 返回
        return mockMultipartFile;
    }
 
    /**
     * 获取文件后缀名称
     * @param file
     * @return
     */
    public static String getFileExtension(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        if (fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0) {
            return fileName.substring(fileName.lastIndexOf(".") + 1);
        } else {
            return "";
        }
    }
 
    /**
     * 获取文件后缀前缀
     * @param file
     * @return
     */
    public static String getFileName(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        if (fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0) {
            return fileName.substring(0,fileName.lastIndexOf("."));
        } else {
            return "";
        }
    }
 
}