欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

文件下载

程序员文章站 2022-06-20 09:00:08
...
protected void readFile(InputStream in, String fileName,
HttpServletResponse response) {
ServletOutputStream out = null;
try {
response
.setContentType("application/octet-stream;charset=ISO8859-1");
response.setHeader("Content-Disposition", "attachment;filename="
+ fileName);
out = response.getOutputStream();
byte[] b = new byte[100];
while (in.read(b) != -1) {
out.write(b);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}