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

来源:互联网 发布:数据迁移方案 详细 编辑:程序博客网 时间:2024/06/02 01:39
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();

0 0
原创粉丝点击