public void exportDmZip(HttpServletResponse response) throws Exception {
OutputStream out = response.getOutputStream();
String fileName = "C:\\Users\\xx\\Desktop\\2.zip";
byte[] data = getByte(fileName);
response.reset();
response.addHeader("Content-Length", ""+data.length);
response.setContentType("application/zip;charset=UTF-8");
response.setHeader("Content-Disposition","attachment;filename="+fileName);
IOUtils.write(data, out);
out.flush();
out.close();
}
private byte[] getByte(String zipFileName) {
try {
FileInputStream fin = new FileInputStream(zipFileName);
ByteArrayOutputStream out = new ByteArrayOutputStream();
int read;
byte[] bytes=new byte[1024];
while((read = fin.read(bytes)) >0){
out.write(bytes, 0, read);
}
fin.close();
bytes = out.toByteArray();
out.close();
return bytes;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
此方法需要本地已生成压缩包,zipFileName为压缩包的路径