Repository navigation
Expand file tree
/
Copy pathHttpUtils.java
More file actions
62 lines (56 loc) · 1.85 KB
/
Copy pathHttpUtils.java
File metadata and controls
62 lines (56 loc) · 1.85 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
package bms.core.common.util;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bms.core.common.Loggers;
/**
* @author xu.jian
*
*/
public class HttpUtils {
public static String getRootPath(HttpServletRequest request) {
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName()
+ ":" + request.getServerPort() + path + "/";
return basePath;
}
public static boolean download(String path, String downFileName,
HttpServletRequest request, HttpServletResponse response) {
java.io.BufferedInputStream bis = null;
java.io.BufferedOutputStream bos = null;
try {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("UTF-8");
Loggers.info("download:" + path + downFileName);
long fileLength = new File(path).length();
response.setContentType("application/x-msdownload;");
response.setHeader("Content-disposition", "attachment; filename="
+ new String(downFileName.getBytes("utf-8"), "ISO8859-1"));
response.setHeader("Content-Length", String.valueOf(fileLength));
bis = new BufferedInputStream(new FileInputStream(path));
bos = new BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead);
}
} catch (Exception e) {
e.printStackTrace();
return false;
} finally {
try {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
}