public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = this.getServletContext().getRealPath(
"XXXX");
System.out.println(path + " --path");
String fileName = path.substring(path.lastIndexOf("\\") + 1);
// 如果下载文件是中文文件,则文件名要经过url编码
response.setHeader("content-disposition", "attachment;filename = "
+ URLEncoder.encode(fileName, "UTF-8"));
InputStream in = null;
OutputStream out = null;
try {
in = new FileInputStream(path);
int len = 0;
byte buffer[] = new byte[1024];
out = response.getOutputStream();
while ((len = in.read(buffer)) > 0) {
out.write(buffer, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
in = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
文件下载
最新推荐文章于 2024-09-24 10:50:41 发布