jiangping
2023-08-17 6365ab0a976afdd247742c9b3dca15deb3a7a7a1
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
package doumeemes.api;
 
import com.alibaba.fastjson.JSONObject;
import doumeemes.biz.system.SystemDictDataBiz;
import doumeemes.core.model.LoginUserInfo;
import doumeemes.core.utils.DateUtil;
import doumeemes.core.utils.aliyun.ALiYunUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
 
/**
 * Controller基类
 * @author Eva.Caesar Liu
 * @date 2022/04/18 18:12
 */
@Slf4j
public class BaseController {
 
    @Autowired
    public SystemDictDataBiz systemDictDataBiz;
 
    /**
     * 获取当前登录用户
     * @author Eva.Caesar Liu
     * @date 2022/04/18 18:12
     */
    protected LoginUserInfo getLoginUser () {
        return (LoginUserInfo)SecurityUtils.getSubject().getPrincipal();
    }
 
 
    public void upload(HttpServletRequest request, HttpServletResponse response, String folder, String bucketName,
                       String access_id, String access_key, String resourcePath, String endpoint) throws Exception {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        Map<String, Object> context = new HashMap<>();
        CommonsMultipartResolver multipartResovler = new CommonsMultipartResolver();
        if (multipartResovler.isMultipart(request)) {
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            Iterator<String> it = multipartRequest.getFileNames();
            while (it.hasNext()) {
                MultipartFile file = multipartRequest.getFile((String) it.next());// file
                // multipartRequest.getFile((String)
                // it.next());
                if (file != null) {
                    // 1、上传到服务器临时文件夹
                    String uploadFileName = file.getOriginalFilename();
                    String originname = uploadFileName;
                    if (originname.lastIndexOf("/") >= 0) {
                        originname = originname.substring(originname.lastIndexOf("/") + 1);
                    }
                    String nfix = "";// 后缀名
                    if (StringUtils.isNotBlank(uploadFileName)) {
                        nfix = uploadFileName.substring(uploadFileName.lastIndexOf("."));
                    }
                    if (StringUtils.equalsIgnoreCase(nfix, ".exe")) {
                        context.put("code", 0);
                        context.put("message", "对不起,文件格式\".exe\"上传有误!");
                        return;
                    }
                    String nowDate = DateUtil.getNowShortDate();// 当前时间(年月日)
                    String fileName = UUID.randomUUID().toString() + nfix;
                    String tempFileName = nowDate + "/" + fileName;
                    String key = folder + tempFileName;// 文件名
                    ALiYunUtil obs = new ALiYunUtil(endpoint,access_id, access_key);
                    if (obs.uploadOnlineObject(file.getInputStream(),bucketName, key,null)) {
                        // 移动成功,返回文件名
                        // sendSuccessMessage(response, resourcePath+key);
                        context.put("error", 0);
                        context.put("code", 200);
                        context.put("url", resourcePath + key);
                        context.put("imgaddr", tempFileName);
                        context.put("imgname", fileName);
                        context.put("originname", originname);
                        writerJson(response, context);
                        return;
                    } else {
                        // 移动失败
                        context.put("code", 0);
                        context.put("message", "上传失败");
                        writerJson(response, context);
                        return;
                    }
                }
 
            }
        }
        context.put("code", 0);
        context.put("message", "上传失败");
        writerJson(response, context);
        return;
    }
 
    public static void writerJson(HttpServletResponse response, Object object) {
        response.setContentType("application/json");
        writer(response, JSONObject.toJSONString(object));
    }
 
    private static void writer(HttpServletResponse response, String str) {
        try {
            StringBuffer result = new StringBuffer();
            //设置页面不缓存
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = null;
            out = response.getWriter();
            out.print(str);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
 
 
 
    public void upload2(MultipartFile file, HttpServletResponse response, String folder, String bucketName,
                       String access_id, String access_key, String resourcePath, String endpoint) throws Exception {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        Map<String, Object> context = new HashMap<>();
        CommonsMultipartResolver multipartResovler = new CommonsMultipartResolver();
 
 
                // multipartRequest.getFile((String)
                // it.next());
                if (file != null) {
                    // 1、上传到服务器临时文件夹
                    String uploadFileName = file.getOriginalFilename();
                    String originname = uploadFileName;
                    if (originname.lastIndexOf("/") >= 0) {
                        originname = originname.substring(originname.lastIndexOf("/") + 1);
                    }
                    String nfix = "";// 后缀名
                    if (StringUtils.isNotBlank(uploadFileName)) {
                        nfix = uploadFileName.substring(uploadFileName.lastIndexOf("."));
                    }
                    if (StringUtils.equalsIgnoreCase(nfix, ".exe")) {
                        context.put("code", 0);
                        context.put("message", "对不起,文件格式\".exe\"上传有误!");
                        return;
                    }
                    String nowDate = DateUtil.getNowShortDate();// 当前时间(年月日)
                    String fileName = UUID.randomUUID().toString() + nfix;
                    String tempFileName = nowDate + "/" + fileName;
                    String key = folder + tempFileName;// 文件名
                    ALiYunUtil obs = new ALiYunUtil(endpoint,access_id, access_key);
                    if (obs.uploadOnlineObject(file.getInputStream(),bucketName, key,null)) {
                        // 移动成功,返回文件名
                        // sendSuccessMessage(response, resourcePath+key);
 
 
 
                        Map<String, Object> data = new HashMap<>();
                        data.put("url", resourcePath + key);
                        data.put("imgaddr", tempFileName);
                        data.put("imgname", fileName);
                        data.put("originname", originname);
 
                        context.put("error", 0);
                        context.put("code", 200);
                        context.put("data", data);
                        context.put("success", true);
                        writerJson(response, context);
                        return;
                    } else {
                        // 移动失败
                        context.put("code", 0);
                        context.put("message", "上传失败");
                        context.put("success", false);
                        writerJson(response, context);
                        return;
                    }
                }
 
 
        context.put("code", 0);
        context.put("message", "上传失败");
        writerJson(response, context);
        return;
    }
}