java打包压缩文件下载到浏览器默认路径

    xiaoxiao2022-07-13  151

    1、controller层

    /** * 压缩打包文件下载到浏览器默认路径 * @return */ @RequestMapping(value="/downloadZipFile") @ResponseBody public Object compressedFile(@RequestParam(value = "docIds",required = false)String docIds){ List<File> files=new ArrayList<>(); List<String> list=new ArrayList<>(); String[] idList =docIds.split(","); for (String id:idList){ list.add(id); } List<DocManager> atttendanchList = docManagerService.selByAttendanceId(list); for (DocManager docManager:atttendanchList){ File file = new File(docManager.getDocPath()+docManager.getFileName()); files.add(file); } //将多个文件压缩打包,下载到默认浏览器 try { File zipFile=FileUtil.fileToZip(files,zipFilePath,ContentUtil.randomGen(6));//将多文件打包为zip文件 FileSystemResource fileSource = new FileSystemResource(zipFile); Object o = FileUtil.downloadFile(fileSource.getInputStream(), fileSource.getFilename()); zipFile.delete();//删除暂时存储的zip压缩包 return o; } catch (IOException e) { return MarkUtil.markRetunMsg(false,e.getMessage()); } }

    2、FileUtil类中fileToZip方法,如下:

    /** * 压缩文件 * @param files * @param zipFilePath * @param fileName * @return */ public static File fileToZip(List<File> files, String zipFilePath, String fileName) { File zipFile = new File(zipFilePath + "/" + fileName + ".zip"); new File(zipFilePath).mkdirs(); FileInputStream fis = null; BufferedInputStream bis = null; FileOutputStream fos = null; ZipOutputStream zos = null; try { fos = new FileOutputStream(zipFile); zos = new ZipOutputStream(new BufferedOutputStream(fos)); byte[] bufs = new byte[1024 * 10]; for (File file : files) { //创建ZIP实体,并添加进压缩包 String name=file.getName(); String reName=name.substring(0,name.length()-6);//修改文件名称,再保存到zip文件中 ZipEntry zipEntry = new ZipEntry(reName);//压缩 zos.putNextEntry(zipEntry);//将每个压缩文件保存到zip文件中 //读取待压缩的文件并写进压缩包里 fis = new FileInputStream(file); bis = new BufferedInputStream(fis, 1024 * 10); int read = 0; while ((read = bis.read(bufs, 0, 1024 * 10)) != -1) { zos.write(bufs, 0, read); } } } catch (FileNotFoundException e) { e.printStackTrace(); throw new RuntimeException(e); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } finally { //关闭流 try { if (null != bis) bis.close(); if (null != zos) zos.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e); } } return zipFile; }

    3、FileUtil类中downloadFile方法,如下:

    /** * 下载文件到默认浏览器 * @param in * @param fileName * @return */ public static ResponseEntity<InputStreamResource> downloadFile(InputStream in,String fileName) { try { byte[] testBytes = new byte[in.available()]; HttpHeaders headers = new HttpHeaders(); headers.add("Cache-Control", "no-cache, no-store, must-revalidate"); headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", MarkUtil.convertFileName(fileName))); headers.add("Pragma", "no-cache"); headers.add("Expires", "0"); return ResponseEntity .ok() .headers(headers) .contentLength(testBytes.length) .contentType(MediaType.parseMediaType("application/octet-stream")) .body(new InputStreamResource(in)); } catch (IOException e) { e.printStackTrace(); } return null; }

    4、MarkUtil类中的convertFileName方法如下:

    /** * chinese code convert * @param fileName * @return */ public static String convertFileName(String fileName) { if(isContainChinese(fileName)) { try { fileName = URLEncoder.encode(fileName, "UTF-8"); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } return fileName; }

    欢迎转载,但是要注释来源,谢谢

    作者:mengmeng233 来源: 原文:https://blog.csdn.net/mengmeng2222222/article/details/90484775 版权声明:本文为博主原创文章,转载请附上博文链接!

    最新回复(0)