Controller @PostMapping("/download") public void download(HttpServletResponse response, @RequestBody BusinessClaims businessClaims) throws IOException { // 文件附件查询 AjaxResult attachment = businessClaimsService.attachment(businessClaims); List<SysFile> data1 = (List<SysFile>) attachment.get("data"); List<File> fileList = new ArrayList<>(); //获取文件名之类的 for (SysFile sysFile : data1) { String filePath = sysFile.getFilePath(); String fileSuffix = sysFile.getFileSuffix(); String fileOldName = sysFile.getFileOldName(); File fileByUrl = getFileByUrl(filePath, fileSuffix,fileOldName); fileList.add(fileByUrl); } response.setHeader("content-type", "application/octet-stream"); response.setContentType("application/octet-stream"); response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=download.zip"); ZipUtils.downloadZip(response.getOutputStream(), fileList); } 工具类
//url转file private File getFileByUrl(String fileUrl, String suffix,String name) { ByteArrayOutputStream outStream = new ByteArrayOutputStream(); BufferedOutputStream stream = null; InputStream inputStream = null; FileOutputStream fileOutputStream = null; File file = new File(name); try { URL imageUrl = new URL(fileUrl); HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection(); conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)"); inputStream = conn.getInputStream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = inputStream.read(buffer)) != -1) { outStream.write(buffer, 0, len); } File file1 = File.createTempFile("zxczzzzzz", suffix); file1.renameTo(file); fileOutputStream = new FileOutputStream(file); stream = new BufferedOutputStream(fileOutputStream); stream.write(outStream.toByteArray()); } catch (Exception e) { } finally { try { if (fileOutputStream != null) fileOutputStream.close(); if (inputStream != null) inputStream.close(); if (stream != null) stream.close(); outStream.close(); } catch (Exception e) { } } return file; } zip 工具类
package com.trustlinksop.common.core.utils.zip; import java.io.*; import java.util.List; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; import static org.springframework.util.FileCopyUtils.BUFFER_SIZE; /** * @Author: Wenbo * @Date: 2023/03/02/11:03 */ public class ZipUtils { /** * 把文件集合打成zip压缩包 * @param srcFiles 压缩文件集合 * @param zipFile zip文件名 * @throws RuntimeException 异常 */ public static void toZip(List<File> srcFiles, File zipFile) throws IOException { if(zipFile == null){ return; } if(!zipFile.getName().endsWith(".zip")){ return; } ZipOutputStream zos = null; FileOutputStream out = new FileOutputStream(zipFile); try { zos = new ZipOutputStream(out); for (File srcFile : srcFiles) { byte[] buf = new byte[BUFFER_SIZE]; zos.putNextEntry(new ZipEntry(srcFile.getName())); int len; // 读取文件并写入到zip中 FileInputStream in = new FileInputStream(srcFile); while ((len = in.read(buf)) != -1) { zos.write(buf, 0, len); zos.flush(); } in.close(); } } catch (Exception e) { e.printStackTrace(); }finally { if (zos != null) { zos.close(); } } } public static void downloadZip(OutputStream outputStream, List<File> fileList){ BufferedInputStream bufferedInputStream = null; ZipOutputStream zipOutputStream = null; FileInputStream in = null; try { zipOutputStream = new ZipOutputStream(outputStream); for (File file : fileList) { ZipEntry zipEntry = new ZipEntry(file.getName()); zipOutputStream.putNextEntry(zipEntry); byte[] buf = new byte[BUFFER_SIZE]; int len; in = new FileInputStream(file); while ((len = in.read(buf)) != -1) { zipOutputStream.write(buf, 0, len); zipOutputStream.flush(); } } zipOutputStream.flush(); zipOutputStream.close(); } catch (IOException e) { e.printStackTrace(); } finally { // 关闭流 try { if (in != null) { System.gc(); in.close(); } if (bufferedInputStream != null) { System.gc(); bufferedInputStream.close(); } if (zipOutputStream != null ) { System.gc(); zipOutputStream.close(); } if (outputStream != null) { System.gc(); outputStream.close(); } if (fileList != null) { try { Thread.currentThread().sleep(500); for (File file : fileList) { System.gc(); boolean delete = file.delete(); System.out.println("delete = " + delete); } } catch (InterruptedException e) { throw new RuntimeException(e); } } } catch (IOException e) { e.printStackTrace(); } } } } 文章思路 先读取出 附件集合 然后 再将url 转为file 文件 再重命名 然后再 通过zip下载 然后删掉本地改名之后的file