文件下载
程序员文章站
2022-06-20 08:57:50
...
String name = request.getParameter("name");
if (name == null || "".equals(name)) {
request.setAttribute("error", "");
RequestDispatcher requestDispatcher = request
.getRequestDispatcher("/error.jsp");
requestDispatcher.forward(request, response);
return;
}
// 应用的Id
String path = request.getParameter("path");
if (path == null || "".equals(path)) {
request.setAttribute("error", "");
RequestDispatcher requestDispatcher = request
.getRequestDispatcher("/error.jsp");
requestDispatcher.forward(request, response);
return;
}
int i = 0;
byte[] b = new byte[1024];
OutputStream out1 = null;
FileInputStream fis = null;
String path1 = props.getProperty("fileStoragePath");
response.setContentType("application/x-download");
String url = path1 + "/" +path;
String fileName = name;
fileName = URLEncoder.encode(fileName, "UTF-8");
response.addHeader("Content-Disposition", "attachment;filename="
+ fileName);
File file = new File(url);
try {
fis = new FileInputStream(file);
out1 = response.getOutputStream();
while ((i = fis.read(b)) > 0) {
out1.write(b, 0, i);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
fis.close();
out1.flush();
out1.close();
} catch (Exception e) {
e.printStackTrace();
}
try {
} catch (Exception e) {
e.printStackTrace();
}
}
上一篇: Linux中强大的top命令
下一篇: 晚饭喝粥能减肥吗,大家别轻信别人的说法