puzhibing
2023-10-08 22199bbdda579861736420fe26c2873ab0f5d21c
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
package com.sinata.modular.system.controller.util;
 
import com.sinata.config.properties.GunsProperties;
import com.sinata.core.base.controller.BaseController;
import com.sinata.core.common.exception.BizExceptionEnum;
import com.sinata.core.exception.GunsException;
import com.sinata.core.util.huawei.obs.ObsUploadUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
 
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.UUID;
 
/**
 * 上传文件
 */
@RestController
@RequestMapping("/upload")
public class UploadUtil extends BaseController {
    private final static Logger log = LoggerFactory.getLogger(UploadUtil.class);
 
    @Autowired
    private GunsProperties gunsProperties;
 
    /**
     * 上传文件(采用上传本地服务器,或OSS服务器)
     * @param picture 文件
     * @param uploadWay 上传方式:1=上传OSS服务器
     * @return
     */
    private String uploadOssOrLocal(MultipartFile picture, Integer... uploadWay){
        String pictureName = null;
        try {
            // 默认上传图片文件方式为本地上传
            if(uploadWay == null || uploadWay.length == 0 || uploadWay[0] == 0) {
                //获取文件名后缀
                String suffix = picture.getOriginalFilename().substring(picture.getOriginalFilename().lastIndexOf("."));
                pictureName = UUID.randomUUID().toString() + suffix;
 
                // 文件目录路径
                String fileSavePath = gunsProperties.getFileUploadPath();
                File file = new File(fileSavePath + pictureName);
                picture.transferTo(file);
            } else {
                pictureName = ObsUploadUtil.obsUpload(picture, "img");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            throw new GunsException(BizExceptionEnum.UPLOAD_ERROR);
        }
        return pictureName;
    }
 
    /**
     * 上传图片(上传到项目的webapp/static/img)
     */
    @RequestMapping("/image")
    public String image(@RequestPart("file") MultipartFile picture) {
        // 上传文件(采用上传本地服务器,或OSS服务器)
        return uploadOssOrLocal(picture ,gunsProperties.getFileUploadWay());
    }
 
    /**
     * 上传文件
     */
    @RequestMapping("/file")
    public String file(@RequestPart("file") MultipartFile picture) {
        // 上传文件(采用上传本地服务器,或OSS服务器)
        return uploadOssOrLocal(picture ,gunsProperties.getFileUploadWay());
    }
 
    /**
     * UEditor编辑器上传图片
     */
    @RequestMapping("/imageUE")
    public String imageUp(@RequestPart("upfile") MultipartFile picture, HttpServletRequest request) {
        String callback = request.getParameter("callback");
        String pictureName = null;
        try {
            // 上传文件(采用上传本地服务器,或OSS服务器)
            pictureName = uploadOssOrLocal(picture ,gunsProperties.getFileUploadWay());
 
            String result = "{'original': '" + picture.getOriginalFilename() + "', 'state': 'SUCCESS', 'url': '" + pictureName + "'}";
            if (callback == null) {
                return result;
            } else {
                return "<script>" + callback + "(" + result + ")</script>";
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            String result = "{'original': '', 'state': '文件上传失败','url': ''}";
            if (callback == null) {
                return result;
            } else {
                return "<script>" + callback + "(" + result + ")</script>";
            }
        }
    }
 
}