DownloadBiz.java
3.87 KB
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
package com.cjs.site.biz.info;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import com.cjs.site.dao.info.DownloadDao;
import com.cjs.site.dao.pub.SysConfigDao;
import com.cjs.site.model.download.DownInfo;
import com.cjs.site.model.pub.SysConfigInfo;
@Service
public class DownloadBiz {
@Value("${${env}.download.path}")
private String downloadPath;
@Autowired
private DownloadDao downloadDao;
@Autowired
private SysConfigDao sysConfigDao;
Logger log = LogManager.getLogger();
/** 文件下载 */
public ResponseEntity<byte[]> download(String url,
HttpServletRequest request) throws IOException {
HttpHeaders head = new HttpHeaders();
File file = new File(downloadPath + "downLoad/" + url);
byte[] bits = FileUtils.readFileToByteArray(file);
head.setContentType(MediaType.APPLICATION_OCTET_STREAM);
head.setContentLength(bits.length);
// 下载的文件名
head.setContentDispositionFormData("attachment",
new String(file.getName().getBytes("UTF-8"), "ISO8859-1"));
return new ResponseEntity<byte[]>(bits, head, HttpStatus.CREATED);
}
public void download2(String key, HttpServletRequest request, HttpServletResponse response) {
ServletOutputStream out = null;
try {
SysConfigInfo config = sysConfigDao.queryBySysKey(key);
if (config == null) {
return;
}
File file = new File(downloadPath + "/download/" + config.getSysValue());
response.setHeader("Content-Disposition",
"attachment;filename=" + this.encodeFileName(request, file.getName()));
response.setContentLength((int) file.length());
response.setContentType("application/octet-stream");
byte[] fileByte = new byte[1024 * 10];
FileInputStream fileInputStream = new FileInputStream(file);
out = response.getOutputStream();
while (fileInputStream.read(fileByte) > 0) {
out.write(fileByte, 0, fileByte.length);
}
} catch (IOException e) {
log.error(e.getMessage());
} finally {
if (out != null) {
try {
out.flush();
out.close();
out = null;
} catch (IOException e) {
log.error(e.getMessage());
}
}
}
}
private String encodeFileName(HttpServletRequest request,
String name) throws UnsupportedEncodingException {
String agent = request.getHeader("USER-AGENT").toLowerCase();
if (agent != null && agent.indexOf("firefox") < 0 && agent.indexOf("safari") < 0) {
return URLEncoder.encode(name, "UTF8");
}
return new String(name.getBytes("UTF-8"), "ISO8859-1");
}
/**添加下载记录*/
public void addDownLoad(DownInfo downInfo) {
downloadDao.addDownInfo(downInfo);
}
/**下载统计*/
public List<DownInfo> getCount() {
return downloadDao.getCount();
}
}