@RequestMapping(value = "/download", method = RequestMethod.POST)
public void plistDownLoad(HttpServletResponse response) throws Exception {
List<String> paths = new ArrayList<>();
paths.add("C:\\Users\\E480\\Desktop\\Study\\casul笔记.txt");
paths.add("C:\\Users\\E480\\Desktop\\Study\\config配置中心笔记.txt");
paths.add("C:\\Users\\E480\\Desktop\\Study\\GateWay.txt");
if (paths.size() != 0) {
String zipFilePath = "D:\\workspace-IDEA\\zip\\我的zip.zip";
ZipOutputStream zipOut = new ZipOutputStream(new FileOutputStream(zipFilePath));
for (String path : paths) {
fileToZip(path, zipOut);
}
zipOut.close();
String fileName = new String(("我的压缩文件.zip").getBytes(),"ISO-8859-1");
response.setHeader("Content-Disposition", "attchment;filename="+fileName);
ServletOutputStream outputStream = response.getOutputStream();
FileInputStream inputStream = new FileInputStream(zipFilePath);
IOUtils.copy(inputStream, outputStream);
inputStream.close();
File fileTempZip = new File(zipFilePath);
fileTempZip.delete();
}
}
public static void fileToZip(String filePath,ZipOutputStream zipOut) throws IOException {
File file = new File(filePath);
String fileName = file.getName();
FileInputStream fileInput = new FileInputStream(filePath);
byte[] bufferArea = new byte[1024 * 10];
BufferedInputStream bufferStream = new BufferedInputStream(fileInput, 1024 * 10);
zipOut.putNextEntry(new ZipEntry(fileName));
int length = 0;
while ((length = bufferStream.read(bufferArea, 0, 1024 * 10)) != -1) {
zipOut.write(bufferArea, 0, length);
}
fileInput.close();
bufferStream.close();
}