文件批量下载------Servlet流
使用servlet流可以实现文件的下载。
关键的实现代码如下:
String zipFileName= "test.zip";
response.setContentType("application/x-msdownload"); // 批量下载
response.setHeader("Content-disposition", "attachment;filename="+ zipFileName);
//获得压缩文件的输出流
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
//下一步就是批量将文件内容写入压缩流中,响应给浏览器客户端
具体的使用压缩流的一个测试类如下:
关键就是需要掌握ZipOutputStream流中putNextEntry以及new ZipEntry这两个API.
注:其实所有的流都差不多,都是从一个数据源中获取输入流,然后将数据写出到输出流即可。