1:导包:
<dependency> <groupId>org.csource</groupId> <artifactId>fastdfs-client-java</artifactId> <version>1.29-RELEASE</version> <scope>compile</scope> </dependency>
2:使用 StorageClient1 进行下载:
public void downloadBatchFile(List<T> list, String downName, HttpServletResponse response) throws IOException { response.setContentType("application/vnd.ms-excel;charset=utf-8"); response.setHeader("Content-Disposition", "attachment;filename=" + new String((downName+ ".zip").getBytes(), "iso-8859-1"));//downName:压缩文件名称 ZipOutputStream zos = new ZipOutputStream(response.getOutputStream()); try { for (T t : list) { ZipEntry newEntry = new ZipEntry(t.getFileName());//下载文件解压的文件名 zos.putNextEntry(newEntry);//遍历文件集进行压缩下载,把文件逐个添加到压缩流中 byte[] content = storageClient1.download_file1(t.getFileUrl());//需要下载文件的路径 zos.write(content, 0, content.length); zos.closeEntry(); } } catch (Exception e) { throw new RuntimeException("zip error from ZipUtils", e); } finally { if (zos != null) { try { zos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
注:记得把IO流关闭