您的位置:首页 > 编程语言 > Java开发

Java使用文件流的方式下载文件

2016-08-19 15:41 405 查看
String path = request.getSession().getServletContext()
.getRealPath("/file");
String filename = "apache-cxf-2.7.10.zip";
String filepath = path + "/" + filename;
InputStream inputstream = new FileInputStream(filepath);
response.reset();
response.setHeader("Content-Disposition", "attachment; filename="
+ filename);
response.setContentType("application/zip");
ServletOutputStream out = response.getOutputStream();
byte[] content = new byte[1024];
int length = 0;
while ((length = inputstream.read(content)) != -1) {
out.write(content, 0, length);
}
out.write(content);
out.flush();
out.close();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: