编程语言 php java

springmvc实现多文件打包压缩下载

java HTML我帮您 1年前  0次浏览
@RequestMapping("/down")
public void down(HttpServletRequest request, HttpServletResponse response){
try {
response.reset();
response.setHeader("Cache-Control", "no-store");
response.addHeader("Content-Disposition", "attachment; filename=html580.zip");
response.setContentType("application/x-zip-compressed");
response.setContentType("application/octet-stream;charset=ISO-8859-1");
Date date = getCurrentDate();

ServletOutputStream fileOut = response.getOutputStream();
ZipOutputStream out = new ZipOutputStream(fileOut);
File inputFolder = new File("这里是需要压缩的文件")
File<> files= inputFolder.listFiles();
for (int i = 0; i < files.length; i++) {
File file = files;
if(file.exists()){
ZipEntry ze = new ZipEntry(file.getName());
ze.setSize(file.length());
ze.setTime(file.lastModified());
out.putNextEntry(ze);
InputStream stream = new FileInputStream(file);
int bytesRead = 0;
byte<> buffer = new byte<8192>;
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
out.write(buffer, 0,bytesRead);
}
stream.close();
}
}
out.close();
fileOut.flush();
fileOut.close();
} catch (IOException e) {
e.printStackTrace();
}
}

发表评论