代码
//导出用户身份证正反面为ZIP
@GetMapping("/getUserCardForZip")
public void getUserCardForZip(String userId, HttpServletRequest request, HttpServletResponse response) throws Exception {
HashMap idCardUrl = (HashMap)zpUserService.getIdCardUrl(userId);
String phone = (String) idCardUrl.get(“phone”);
String fan = (String) idCardUrl.get(“fan”);
String zheng = (String) idCardUrl.get(“zheng”);
String [] urls={fan,zheng};
response.setContentType(MediaType.APPLICATION_OCTET_STREAM.toString());
response.setHeader(“Content-Disposition”,“attachment; filename=”"+phone+".zip"");
ZipOutputStream outStream = new ZipOutputStream(response.getOutputStream());
for(String u:urls){
String ext=u.substring(u.lastIndexOf("."), u.length());
String fileName=""+System.currentTimeMillis()+ext;
URL url = new URL(u);
URLConnection c = url.openConnection();
c.connect();
int fileLength=c.getContentLength();
System.out.println("file size:"+fileLength);
InputStream is = c.getInputStream();
outStream.putNextEntry(new ZipEntry(fileName));
byte[] buffer=new byte[2048];
int len;
while((len=is.read(buffer))!=-1){
outStream.write(buffer,0,len);
}
//刷新流
try {
if (outStream != null) {
outStream.flush();
}
} catch (Exception e) {
e.printStackTrace();
}
}
//关闭流,
try {
if (outStream != null) {
outStream.close();
}
} catch (Exception e) {
e.printStackTrace();
}
long end=System.currentTimeMillis();
}