xiangpei
2024-07-11 5073a245f53fd5ca936e779be8c6b9b19d42f67d
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package com.ycl.jxkg.controller.admin;
 
 
import com.ycl.jxkg.base.BaseApiController;
import com.ycl.jxkg.base.Result;
import com.ycl.jxkg.config.RuoYiConfig;
import com.ycl.jxkg.config.property.SystemConfig;
import com.ycl.jxkg.domain.vo.admin.file.UeditorConfigVO;
import com.ycl.jxkg.domain.vo.admin.file.UploadResultVO;
import com.ycl.jxkg.service.FileUpload;
import com.ycl.jxkg.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.UUID;
 
 
@RequestMapping("/api/admin/upload")
@RestController("AdminUploadController")
public class UploadController extends BaseApiController {
 
    private final FileUpload fileUpload;
    private final SystemConfig systemConfig;
    private static final Logger logger = LoggerFactory.getLogger(UploadController.class);
    private static final String IMAGE_UPLOAD = "imgUpload";
    private static final String IMAGE_UPLOAD_FILE = "upFile";
    private final UserService userService;
    private final RuoYiConfig ruoYiConfig;
 
 
 
    @Autowired
    public UploadController(FileUpload fileUpload, SystemConfig systemConfig, UserService userService, RuoYiConfig ruoYiConfig) {
        this.fileUpload = fileUpload;
        this.systemConfig = systemConfig;
        this.userService = userService;
        this.ruoYiConfig = ruoYiConfig;
    }
 
    @ResponseBody
    @RequestMapping("/configAndUpload")
    public Object upload(HttpServletRequest request, HttpServletResponse response) {
        String action = request.getParameter("action");
        if (action.equals(IMAGE_UPLOAD)) {
            MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
            MultipartFile file = multipartHttpServletRequest.getFile(IMAGE_UPLOAD_FILE);
            // 检查文件是否为空
            if (file.isEmpty()) {
                return Result.fail(500, "上传的文件为空");
            }
            try {
                // 获取文件名
                String originalFileName = StringUtils.cleanPath(file.getOriginalFilename());
                String randomName = UUID.randomUUID().toString().replace("-", "") + originalFileName.substring(originalFileName.lastIndexOf("."));
                // 指定文件存储路径
                String uploadDir = ruoYiConfig.getUrl(); // 修改为您希望存储的目录
                // 如果目录不存在,则创建目录
                File dir = new File(uploadDir);
                if (!dir.exists()) {
                    dir.mkdirs();
                }
                // 构建目标文件的路径
                String filePath = uploadDir + "/" + randomName;
                // 将文件保存到目标位置
                file.transferTo(new File(filePath));
                // 返回成功响应
                HashMap hashMap = new HashMap(2);
                hashMap.put("name", originalFileName);
                hashMap.put("url", randomName);
 
                String imageType = randomName.substring(randomName.lastIndexOf("."));
                UploadResultVO uploadResultVO = new UploadResultVO();
                uploadResultVO.setOriginal(randomName);
                uploadResultVO.setName(randomName);
                uploadResultVO.setUrl("/api/files/" + randomName);
                uploadResultVO.setSize(10L);
                uploadResultVO.setType(imageType);
                uploadResultVO.setState("SUCCESS");
                return uploadResultVO;
            } catch (IOException e) {
                e.printStackTrace();
                // 返回失败响应
                return Result.fail(500, "文件上传失败");
            }
        } else {
            UeditorConfigVO ueditorConfigVO = new UeditorConfigVO();
            ueditorConfigVO.setImageActionName(IMAGE_UPLOAD);
            ueditorConfigVO.setImageFieldName(IMAGE_UPLOAD_FILE);
            ueditorConfigVO.setImageMaxSize(2048000L);
            ueditorConfigVO.setImageAllowFiles(Arrays.asList(".png", ".jpg", ".jpeg", ".gif", ".bmp"));
            ueditorConfigVO.setImageCompressEnable(true);
            ueditorConfigVO.setImageCompressBorder(1600);
            ueditorConfigVO.setImageInsertAlign("none");
            ueditorConfigVO.setImageUrlPrefix("");
            ueditorConfigVO.setImagePathFormat("");
            return ueditorConfigVO;
        }
    }
 
 
    @RequestMapping("/image")
    @ResponseBody
    public Result questionUploadAndReadExcel(HttpServletRequest request) {
        MultipartHttpServletRequest multipartHttpServletRequest = (MultipartHttpServletRequest) request;
        MultipartFile multipartFile = multipartHttpServletRequest.getFile("file");
        long attachSize = multipartFile.getSize();
        String imgName = multipartFile.getOriginalFilename();
        try (InputStream inputStream = multipartFile.getInputStream()) {
            String filePath = fileUpload.uploadFile(inputStream, attachSize, imgName);
            userService.changePicture(getCurrentUser(), filePath);
            return Result.ok(filePath);
        } catch (IOException e) {
            return Result.fail(2, e.getMessage());
        }
    }
 
 
}