ludc
2024-07-04 8c4b79d994c5a95dd33d4b0313e2f2d63985e074
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
package com.vci.starter.web.util;
 
import com.vci.starter.web.constant.VConstant;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
 
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * 在controller层使用的工具
 * @author weidy
 * @date 2020/2/19
 */
public class ControllerUtil {
 
    /**
     * 日志对象
     */
    private static Logger logger = LoggerFactory.getLogger(ControllerUtil.class);
 
    /**
     * 将文件写入到返回流中
     * @param response 响应对象
     * @param fileName 文件名称
     * @throws FileNotFoundException 文件没有找到
     * @throws IOException 读取文件出错,或者写数据出错
     */
    public static void writeFileToResponse(HttpServletResponse response,String fileName) throws FileNotFoundException,IOException{
        writeFileToResponse(response,fileName,true);
    }
 
    /**
     * 将文件写入到返回流中
     * @param response 响应对象
     * @param fileName 文件名称
     * @param deleteFile 下载完成后是否删除文件,true表示删除,另外这个文件的上级文件夹也会被删除,因此临时文件夹一般都添加了随机名称的文件夹
     * @throws FileNotFoundException 文件没有找到
     * @throws IOException 读取文件出错,或者写数据出错
     */
    public static void writeFileToResponse(HttpServletResponse response,String fileName,boolean deleteFile) throws FileNotFoundException,IOException{
        writeFileToResponse(response,fileName,null,deleteFile);
    }
 
    /**
     * 将文件写入到返回流中
     * @param response 响应对象
     * @param fileName 文件名称
     * @param deleteFile 下载完成后是否删除文件,true表示删除,另外这个文件的上级文件夹也会被删除,因此临时文件夹一般都添加了随机名称的文件夹
     * @param showName 显示文件名,即用户看到的名称,否则直接为原文件的名称
     * @throws FileNotFoundException 文件没有找到
     * @throws IOException 读取文件出错,或者写数据出错
     */
    public static void writeFileToResponse(HttpServletResponse response,String fileName,String showName,boolean deleteFile) throws FileNotFoundException,IOException {
        File file = new File(fileName);
        if(!file.exists()){
            throw new FileNotFoundException(fileName);
        }
        writeFileToResponse(response,file,showName,deleteFile);
    }
 
    /**
     * 将文件写入到返回流中
     * @param response 响应对象
     * @param file 文件对象
     * @throws FileNotFoundException 文件没有找到
     * @throws IOException 读取文件出错,或者写数据出错
     */
    public static void writeFileToResponse(HttpServletResponse response, File file) throws FileNotFoundException,IOException{
        writeFileToResponse(response,file,true);
    }
 
    /**
     * 将文件写入到返回流中
     * @param response 响应对象
     * @param file 文件对象
     * @param deleteFile 下载完成后是否删除文件,true表示删除,另外这个文件的上级文件夹也会被删除,因此临时文件夹一般都添加了随机名称的文件夹
     * @throws FileNotFoundException 文件没有找到
     * @throws IOException 读取文件出错,或者写数据出错
     */
    public static void writeFileToResponse(HttpServletResponse response,File file,boolean deleteFile) throws FileNotFoundException,IOException{
        writeFileToResponse(response,file,null,deleteFile);
    }
 
    /**
     * 将文件写入到返回流中
     * @param response 响应对象
     * @param file 文件对象
     * @param deleteFile 下载完成后是否删除文件,true表示删除,另外这个文件的上级文件夹也会被删除,因此临时文件夹一般都添加了随机名称的文件夹
     * @param showName 显示文件名,即用户看到的名称,否则直接为原文件的名称
     * @throws FileNotFoundException 文件没有找到
     * @throws IOException 读取文件出错,或者写数据出错
     */
    public static void writeFileToResponse(HttpServletResponse response,File file,String showName,boolean deleteFile) throws FileNotFoundException,IOException {
        writeFileToResponse(response,file,showName,deleteFile,null);
    }
    /**
     * 将文件写入到返回流中
     * @param response 响应对象
     * @param file 文件对象
     * @param deleteFile 下载完成后是否删除文件,true表示删除,另外这个文件的上级文件夹也会被删除,因此临时文件夹一般都添加了随机名称的文件夹
     * @param showName 显示文件名,即用户看到的名称,否则直接为原文件的名称
     * @throws FileNotFoundException 文件没有找到
     * @throws IOException 读取文件出错,或者写数据出错
     */
    public static void writeFileToResponse(HttpServletResponse response,File file,String showName,boolean deleteFile,String contentType) throws FileNotFoundException,IOException {
        VciBaseUtil.alertNotNull(file, "文件对象");
        if (!file.exists()) {
            throw new FileNotFoundException(file.getName());
        }
        if (StringUtils.isBlank(showName)) {
            showName = file.getName();
        }
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            writeFileToResponse(response, in, showName, contentType);
        } catch (IOException e) {
            //有可能客户端的链接
            if (logger.isErrorEnabled()) {
                logger.error("写入文件到响应流出错", e);
            }
        } finally {
            IOUtils.closeQuietly(in);
            if (deleteFile) {
                File parentFile = file.getParentFile();
                file.delete();
                parentFile.delete();
            }
        }
    }
 
 
    /**
     * 将输入流写入到返回流中
     * @param response  响应对象
     * @param ins 输入流
     * @param showName 名称
     * @throws IOException 拷贝出错的时候会抛出异常
     */
    public static void writeFileToResponse(HttpServletResponse response,InputStream ins,String showName) throws IOException{
        writeFileToResponse(response,ins,showName,null);
    }
 
    /**
     * 将输入流写入到返回流中
     * @param response  响应对象
     * @param ins 输入流
     * @param showName 名称
     * @throws IOException 拷贝出错的时候会抛出异常
     */
    public static void writeFileToResponse(HttpServletResponse response,InputStream ins,String showName,String contentType ) throws IOException{
        if(StringUtils.isBlank(contentType)) {
            contentType = "application/force-download";
        }
        if(StringUtils.isBlank(showName)){
            showName = "下载文件";
        }
        // 设置强制下载不打开
        response.setContentType(contentType);
        try{
            String fileName = URLEncoder.encode(showName, "UTF8");
            response.addHeader("Content-Disposition", "attachment; filename="+ fileName+ ";filename*=utf-8''" + fileName);
        }catch(Exception e){
            if(logger.isErrorEnabled()){
                logger.error("设置文件的名称到响应流的时候出错",e);
            }
        }
        response.setCharacterEncoding("UTF-8");
        Cookie cookie = new Cookie("fileDownload", "true");
        cookie.setPath("/");
        response.addCookie(cookie);
        try{
            IOUtils.copy(ins,response.getOutputStream());
        } catch (IOException e) {
            //有可能客户端的链接
            if(logger.isErrorEnabled()){
                logger.error("写入文件到响应流出错",e);
            }
            throw e;
        }finally {
 
            IOUtils.closeQuietly(ins);
        }
    }
 
    /**
     * 将输入流写入到返回流中
     * @param response  响应对象
     * @param data 数据的信息
     * @throws IOException 拷贝出错的时候会抛出异常
     */
    public static void writeDataToResponse(HttpServletResponse response,byte[] data,String contentType) throws IOException {
        if (StringUtils.isBlank(contentType)) {
            contentType = "application/force-download";
        }
        response.setContentType(contentType);
        response.setCharacterEncoding("UTF-8");
        //错误时也需要这个参数
        Cookie cookie = new Cookie("fileDownload", "true");
        cookie.setPath("/");
        response.addCookie(cookie);
        try {
            response.getOutputStream().write(data);
        } catch (IOException e) {
            //有可能客户端的链接
            if (logger.isErrorEnabled()) {
                logger.error("写入文件到响应流出错", e);
            }
            throw e;
        }
    }
 
    /**
     * 将错误的信息输入流写入到返回流中
     * @param response  响应对象
     * @param data 数据的信息
     * @throws IOException 拷贝出错的时候会抛出异常
     */
    public static void writeDataToResponse(HttpServletResponse response,String fileName,byte[] data,String contentType) throws IOException {
        if (StringUtils.isBlank(contentType)) {
            contentType = "application/force-download";
        }
        response.setContentType(contentType);
        response.setCharacterEncoding("UTF-8");
        //错误时也需要这个参数
        response.addHeader("Content-Disposition", "attachment; filename="+ fileName+ ";filename*=utf-8''" + fileName);
        Cookie cookie = new Cookie("fileDownload", "true");
        cookie.setPath("/");
        response.addCookie(cookie);
        try {
            response.getOutputStream().write(data);
        } catch (IOException e) {
            //有可能客户端的链接
            if (logger.isErrorEnabled()) {
                logger.error("写入文件到响应流出错", e);
            }
            throw e;
        }
    }
 
    /**
     * 将环境变量中的某个文件写到返回流中
     * @param response 响应对象
     * @param classPathFileName 在环境变量里的文件名称
     * @throws FileNotFoundException 文件没有找到
     * @throws IOException 读取文件出错,或者写数据出错
     */
    public static void writeClasspathFileToResponse(HttpServletResponse response,String classPathFileName) throws FileNotFoundException,IOException{
        writeClasspathFileToResponse(response,classPathFileName,null);
    }
 
    /**
     * 将环境变量中的某个文件写到返回流中
     * @param response 响应对象
     * @param classPathFileName 在环境变量里的文件名称
     * @param showName 显示名称
     * @throws FileNotFoundException 文件没有找到
     * @throws IOException 读取文件出错,或者写数据出错
     */
    public static void writeClasspathFileToResponse(HttpServletResponse response,String classPathFileName,String showName) throws FileNotFoundException,IOException{
        InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(classPathFileName);
        if(StringUtils.isBlank(showName)){
            showName = classPathFileName.contains("/")?classPathFileName.substring(classPathFileName.lastIndexOf("/") + 1):classPathFileName;
        }
        try {
            writeFileToResponse(response, in, showName);
        }catch (IOException e){
            throw e;
        }finally {
            IOUtils.closeQuietly(in);
        }
    }
 
    /**
     * 获取IP地址,通过request
     * @param request http请求对象
     * @return ip地址
     */
    public static String getClientInfo(HttpServletRequest request){
        String ip = request.getHeader("X-Forwarded-For");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_CLIENT_IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        if (ip == null || ip.length() == 0 || ip.indexOf("0:0:0:0:0:0:0:1") >-1) {//0:0:0:0:0:0:0:1是本机在访问
            ip = "127.0.0.1";
        }
        return ip;
    }
 
    /**
     * 下载错误文件的映射内容
     */
    public static final Map<String/**主键**/,String/**文件路径**/> tempFileForDownloadMap = new ConcurrentHashMap<>();
 
 
    /**
     * 设置查询总数
     * @param request 请求对象
     * @param isQueryTotal 是否查询总数
     */
    public static void setQueryTotal(HttpServletRequest request, boolean isQueryTotal){
        WebThreadLocalUtil.setNeedQueryTotalInThread(isQueryTotal?"true":"false");
        //request.setAttribute(webProperties.getQueryTotalSessionName(), isQueryTotal);
    }
 
    /**
     * 放置错误的文件
     * @param errorFile 错误的文件
     * @return 唯一标识
     */
    public static String putErrorFile(String errorFile){
        String uuid = VciBaseUtil.getPk();
        tempFileForDownloadMap.put(uuid,errorFile);
        return uuid;
    }
 
    /**
     * 下载错误的文件(仅限于excel)
     * @param response 响应对象
     * @param uuid 唯一的标识
     * @throws IOException 写入文件出错的时候抛出异常
     */
    public static void downloadErrorFile(HttpServletResponse response,String uuid) throws IOException{
        String errorFile = tempFileForDownloadMap.getOrDefault(uuid, "");
        try {
            if (StringUtils.isNotBlank(errorFile)) {
                ControllerUtil.writeFileToResponse(response, new File(errorFile), null, true, "application/msexcel");
            }
        }finally {
            tempFileForDownloadMap.remove(uuid);
        }
    }
 
    /**
     * 内容编码
     *
     * @param str 内容
     * @return 编码后的内容
     */
    public static String urlEncode(String str) {
        try {
            return URLEncoder.encode(str, VConstant.UTF8);
        } catch (UnsupportedEncodingException e) {
            return StringUtils.EMPTY;
        }
    }
 
    /**
     * 内容解码
     *
     * @param str 内容
     * @return 解码后的内容
     */
    public static String urlDecode(String str) {
        try {
            return URLDecoder.decode(str, VConstant.UTF8);
        } catch (UnsupportedEncodingException e) {
            return StringUtils.EMPTY;
        }
    }
}