前台js
var _downloadZipAjax = function(obj,url){ var elemIF = document.createElement("iframe"); elemIF.src = url; elemIF.style.display = "none"; document.body.appendChild(elemIF); };
后台
@RequestMapping(value = "/downloadZipAjax") public ResponseEntity<byte[]> downloadZipAjax(String period, String cNo) throws Exception { ZipTool zt = new ZipTool(); ReportErrorInfo reportErrorInfo = new ReportErrorInfo(); reportErrorInfo.setNo(cNo); reportErrorInfo.setPeriod(period); reportErrorInfo = reportErrorInfoFeign.getByErrorInfo(reportErrorInfo); //zu.addFile(filepath);//添加文件 if(reportErrorInfo.getFileAddr() != null && !"".equals(reportErrorInfo.getFileAddr())){ String path = UploadUtil.getFileUploadPath(); String[] fileAddrs = reportErrorInfo.getFileAddr().split(","); for(int i =0;i<fileAddrs.length;i++){ zt.addFile(path+fileAddrs[i]); } } String fileName = "AA企业"+"_"+ WaterIdUtil.getWaterId()+".zip"; fileName = new String(fileName.getBytes(),"ISO8859-1"); InputStream inputStream = new ByteArrayInputStream(zt.getOutputStream()); HttpHeaders headers = new HttpHeaders(); headers.add("Content-Disposition", "attchement;filename="+fileName); HttpStatus statusCode = HttpStatus.OK; ResponseEntity<byte[]> entity = new ResponseEntity<byte[]>(toByteArray(inputStream), headers, statusCode); return entity; }
ZipTool 是一个封装的工具类
public class ZipTool { private ByteArrayOutputStream outputStream = null; private ZipOutputStream zip = null; public ZipTool() { outputStream = new ByteArrayOutputStream(); zip = new ZipOutputStream(outputStream); } /** * 添加文件到压缩包 * @param filepath */ public void addFile(String filepath) { File file = new File(filepath); String filename = file.getName(); try { zip.putNextEntry(new ZipEntry(filename)); byte[] filebyte = file2byte(file); IOUtils.write(filebyte, zip); zip.closeEntry(); } catch (IOException e) { e.printStackTrace(); } } /** * 添加文件到压缩包,并指定新的文件名 * @param filepath */ public void addFileReName(String filepath,String NewFileName) { File file = new File(filepath); try { zip.putNextEntry(new ZipEntry(NewFileName)); byte[] filebyte = file2byte(file); IOUtils.write(filebyte, zip); zip.closeEntry(); } catch (IOException e) { e.printStackTrace(); } } /** * 添加文件到压缩包,并指定压缩包内路径 * @param filepath * @param inZipPath */ public void addFile(String filepath,String inZipPath) { File file = new File(filepath); try { zip.putNextEntry(new ZipEntry(inZipPath)); byte[] filebyte = file2byte(file); IOUtils.write(filebyte, zip); zip.closeEntry(); } catch (IOException e) { e.printStackTrace(); } } /** * 将压缩包生成到指定位置 * @param targetFileName */ public void outZip(String targetFileName) { String fileOutName = targetFileName + ".zip"; FileOutputStream fos = null; try { fos = new FileOutputStream(fileOutName); IOUtils.closeQuietly(zip); IOUtils.write(outputStream.toByteArray(), fos); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { close(); } } /** * File 转换成 byte * @param file * @return */ public static byte[] file2byte(File file) { byte[] buffer = null; try { FileInputStream fis = new FileInputStream(file); ByteArrayOutputStream bos = new ByteArrayOutputStream(); byte[] b = new byte[1024]; int n; while ((n = fis.read(b)) != -1) { bos.write(b, 0, n); } fis.close(); bos.close(); buffer = bos.toByteArray(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return buffer; } /** * 获取输出流字节 * @return byte[] */ public byte[] getOutputStream() { IOUtils.closeQuietly(zip); byte[] byteArray = outputStream.toByteArray(); close(); return byteArray; } /** * 关闭流 */ public void close() { try { if (outputStream != null) { outputStream.close(); } if (zip != null) { zip.close(); } } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) { ZipTool zt = new ZipTool(); zt.close(); } }