@PostMapping("export1") public ResponseEntity<StreamingResponseBody> fileEport1(MultipartFile[] file) throws Exception { byte[] buf = new byte[2048]; StreamingResponseBody stream = new StreamingResponseBody() { @Override public void writeTo(OutputStream outputStream) throws IOException { ZipOutputStream zos = new ZipOutputStream(outputStream); for (int i = 0; i <= file.length - 1; i++) { zos.putNextEntry(new ZipEntry(file[i].getOriginalFilename())); int len; InputStream inputStream = file[i].getInputStream(); while ((len = inputStream.read(buf)) != -1) { zos.write(buf, 0, len); } zos.closeEntry(); } zos.close(); } }; String zipFileName = "test.zip"; return ResponseEntity.ok() .header(HttpHeaders.ACCESS_CONTROL_EXPOSE_HEADERS, HttpHeaders.CONTENT_DISPOSITION) .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + zipFileName) .contentType(MediaType.parseMediaType("application/zip")) .body(stream); }
记一次奇葩需求批量上传文件压缩返回
最新推荐文章于 2024-04-28 00:56:31 发布