KKSU
2024-03-27 f10afe3a9aab4e962b3ec456b8a707163e11f9bd
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
87
88
89
90
91
92
93
94
95
96
package cc.mrbird.febs.dapp.controller;
 
 
import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cn.hutool.core.lang.UUID;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FileCopyUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
 
@Slf4j
@Validated
@RestController
@RequiredArgsConstructor
@RequestMapping(value = "/admin/goods")
public class AdminMallGoodsController extends BaseController {
 
    /**
     * 通用上传请求
     */
 
    @Value("${static.resource.url}")
    private String resourceUrl;
 
    @Value("${static.resource.path}")
    private String resourcePath;
 
    @PostMapping("/uploadFileBase64")
    @ControllerEndpoint(operation = "图片上传", exceptionMessage = "上传失败")
    public Map<String,Object> upload(MultipartFile file) throws IOException {
        if (file == null) {
            return new FebsResponse().message("上传文件为空");
        }
        // 文件保存目录路径
        String savePath = resourcePath;
        // 文件保存目录URL
        String saveUrl = resourceUrl;
        // 检查目录
        File uploadDir = new File(savePath);
        if (!uploadDir.isDirectory()) {
            uploadDir.mkdir();
        }
 
        // 获得文件的后缀
        String fileName = file.getOriginalFilename();
        String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1);
        String newFileName = UUID.randomUUID().toString() + "." + fileExt;
        if (isImage(file.getInputStream())) {
            Thumbnails.of(file.getInputStream())
                    // 图片大小(长宽)压缩比例 从0-1,1表示原图
                    .scale(1f)
                    // 图片质量压缩比例 从0-1,越接近1质量越好
                    .outputQuality(0.9f)
                    .toOutputStream(new FileOutputStream(savePath + newFileName));
        } else {
            File uploadedFile = new File(savePath, newFileName);
            try {
                //存文件
                FileCopyUtils.copy(file.getBytes(), uploadedFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        String visitPath = (saveUrl + newFileName);
        Map<String,Object> map = new HashMap<String,Object>();
        Map<String,Object> map2 = new HashMap<String,Object>();
        map.put("code",0);//0表示成功,1失败
        map.put("msg","上传成功");//提示消息
        map.put("data",map2);
        map2.put("src",visitPath);//图片url
        map2.put("title",fileName);//图片名称,这个会显示在输入框里
        return map;
    }
 
    private boolean isImage(InputStream inputStream) {
        BufferedImage read = null;
        try {
            read = ImageIO.read(inputStream);
        } catch (IOException e) {
            return false;
        }
        return read != null;
    }
}