commit | author | age
|
9bcb19
|
1 |
/* |
I |
2 |
Copyright [2020] [https://www.xiaonuo.vip] |
|
3 |
|
|
4 |
Licensed under the Apache License, Version 2.0 (the "License"); |
|
5 |
you may not use this file except in compliance with the License. |
|
6 |
You may obtain a copy of the License at |
|
7 |
|
|
8 |
http://www.apache.org/licenses/LICENSE-2.0 |
|
9 |
|
|
10 |
Unless required by applicable law or agreed to in writing, software |
|
11 |
distributed under the License is distributed on an "AS IS" BASIS, |
|
12 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 |
See the License for the specific language governing permissions and |
|
14 |
limitations under the License. |
|
15 |
|
|
16 |
Snowy采用APACHE LICENSE 2.0开源协议,您在使用过程中,需要注意以下几点: |
|
17 |
|
|
18 |
1.请不要删除和修改根目录下的LICENSE文件。 |
|
19 |
2.请不要删除和修改Snowy源码头部的版权声明。 |
|
20 |
3.请保留源码和相关描述文件的项目出处,作者声明等。 |
|
21 |
4.分发源码时候,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
22 |
5.在修改包名,模块名称,项目代码等时,请注明软件出处 https://gitee.com/xiaonuobase/snowy |
|
23 |
6.若您的项目无法满足以上几点,可申请商业授权,获取Snowy商业授权许可,请在官网购买授权,地址为 https://www.xiaonuo.vip |
|
24 |
*/ |
|
25 |
package vip.xiaonuo.sys.modular.file.util; |
|
26 |
|
|
27 |
import cn.hutool.core.io.FileUtil; |
|
28 |
import cn.hutool.core.io.IoUtil; |
|
29 |
import cn.hutool.core.util.CharsetUtil; |
|
30 |
import cn.hutool.core.util.URLUtil; |
|
31 |
import cn.hutool.log.Log; |
|
32 |
import vip.xiaonuo.core.context.requestno.RequestNoContext; |
|
33 |
import vip.xiaonuo.core.exception.ServiceException; |
|
34 |
import vip.xiaonuo.sys.modular.file.enums.SysFileInfoExceptionEnum; |
|
35 |
|
|
36 |
import javax.servlet.http.HttpServletResponse; |
|
37 |
import java.io.File; |
|
38 |
import java.io.IOException; |
|
39 |
import java.io.InputStream; |
|
40 |
import java.io.UnsupportedEncodingException; |
|
41 |
import java.net.URL; |
|
42 |
import java.net.URLEncoder; |
|
43 |
|
|
44 |
/** |
|
45 |
* 文件下载工具类 |
|
46 |
* |
|
47 |
* @author xuyuxiang |
|
48 |
* @date 2020/8/5 21:45 |
|
49 |
*/ |
|
50 |
public class DownloadUtil { |
|
51 |
|
|
52 |
private static final Log log = Log.get(); |
|
53 |
|
|
54 |
public static void download(String fileName, byte[] fileBytes, HttpServletResponse response) { |
|
55 |
try { |
|
56 |
response.reset(); |
|
57 |
response.setHeader("Content-Disposition", "attachment; filename=\"" + URLUtil.encode(fileName) + "\""); |
|
58 |
response.addHeader("Content-Length", "" + fileBytes.length); |
|
59 |
response.setHeader("Access-Control-Allow-Origin", "*"); |
|
60 |
response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); |
|
61 |
response.setContentType("application/octet-stream;charset=UTF-8"); |
|
62 |
IoUtil.write(response.getOutputStream(), true, fileBytes); |
|
63 |
} catch (IOException e) { |
|
64 |
log.error(">>> 下载文件异常,请求号为:{},具体信息为:{}", RequestNoContext.get(), e.getMessage()); |
|
65 |
throw new ServiceException(SysFileInfoExceptionEnum.DOWNLOAD_FILE_ERROR); |
|
66 |
} |
|
67 |
} |
|
68 |
|
|
69 |
/** |
|
70 |
* 下载文件 |
|
71 |
* |
|
72 |
* @param file 要下载的文件 |
|
73 |
* @param response 响应 |
|
74 |
* @author xuyuxiang |
|
75 |
* @date 2020/8/5 21:46 |
|
76 |
*/ |
|
77 |
public static void download(File file, HttpServletResponse response) { |
|
78 |
// 获取文件字节 |
|
79 |
byte[] fileBytes = FileUtil.readBytes(file); |
|
80 |
//获取文件名称 |
|
81 |
String fileName; |
|
82 |
try { |
|
83 |
fileName = URLEncoder.encode(file.getName(), CharsetUtil.UTF_8); |
|
84 |
} catch (UnsupportedEncodingException e) { |
|
85 |
log.error(">>> 下载文件异常,请求号为:{},具体信息为:{}", RequestNoContext.get(), e.getMessage()); |
|
86 |
throw new ServiceException(SysFileInfoExceptionEnum.DOWNLOAD_FILE_ERROR); |
|
87 |
} |
|
88 |
//下载文件 |
|
89 |
download(fileName, fileBytes, response); |
|
90 |
} |
|
91 |
|
|
92 |
/** |
|
93 |
* 将url的文件下载到目标文件 |
|
94 |
* |
|
95 |
* @param url 下载url |
|
96 |
* @param file 目标文件 |
|
97 |
* @author xuyuxiang |
|
98 |
* @date 2021/3/25 16:51 |
|
99 |
*/ |
|
100 |
public static void downloadToFile(String url, File file) { |
|
101 |
if (url == null || url.isEmpty()) throw new ServiceException(SysFileInfoExceptionEnum.DOWNLOAD_FILE_ERROR); |
|
102 |
|
|
103 |
if (file == null) throw new ServiceException(SysFileInfoExceptionEnum.NOT_EXISTED_FILE); |
|
104 |
|
|
105 |
try { |
|
106 |
URL uri = new URL(url); |
|
107 |
java.net.HttpURLConnection connection = (java.net.HttpURLConnection) uri.openConnection(); |
|
108 |
InputStream stream = connection.getInputStream(); |
|
109 |
|
|
110 |
if (stream == null) { |
|
111 |
throw new ServiceException(SysFileInfoExceptionEnum.FILE_STREAM_ERROR); |
|
112 |
} |
|
113 |
FileUtil.writeFromStream(stream, file); |
|
114 |
connection.disconnect(); |
|
115 |
} catch (Exception e) { |
|
116 |
log.error(">>> 下载文件异常,请求号为:{},具体信息为:{}", RequestNoContext.get(), e.getMessage()); |
|
117 |
throw new ServiceException(SysFileInfoExceptionEnum.DOWNLOAD_FILE_ERROR); |
|
118 |
} |
|
119 |
} |
|
120 |
} |