Spring Boot 生成壓縮文件,並提供下載

生成文件

@Override
    public File downloadZipFile(Integer cid) {
        List<AwardCert> certFiles = lambdaQuery()
                .eq(AwardCert::getCid, cid)
                .eq(AwardCert::getHandlingStatus, 2)
                .list();
        FileOutputStream fos = null;
        ZipOutputStream zipOut = null;
        File zipFile = null;
        try {
            zipFile = new File(MicroUUID.randomUUID15() + ".zip");
            fos = new FileOutputStream(zipFile);
            zipOut = new ZipOutputStream(fos);
            for (AwardCert templetFile : certFiles) {
                File fileToZip = null;
                FileInputStream fis = null;
                try {
                    fileToZip = FileUtil.createTmpFile();
                    String urlPath = templetFile.getFileUrl();
                    FileUtil.copyURLToFile(new URL(urlPath), fileToZip);
                    fis = new FileInputStream(fileToZip);
                    ZipEntry zipEntry;
                    if (templetFile.getType() == 1) {
                        zipEntry = new ZipEntry(templetFile.getContestantName() + ".jpg");
                    } else {
                        zipEntry = new ZipEntry(templetFile.getTeacher() + ".jpg");
                    }
                    zipOut.putNextEntry(zipEntry);
                    byte[] bytes = new byte[1024];
                    int length;
                    while ((length = fis.read(bytes)) >= 0) {
                        zipOut.write(bytes, 0, length);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (fileToZip != null) {
                        boolean delete = fileToZip.delete();
                    }
                    if (fis != null) {
                        fis.close();
                    }
                }
            }

            return zipFile;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (zipOut != null) {
                try {
                    zipOut.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return zipFile;
    }

下載

@RequestMapping(value = "/downloadAll", method = RequestMethod.GET)
    public ResponseEntity<Object> downloadAll(Integer cid) {
        try {
            File zipFile = awardCertService.downloadZipFile(cid);
            InputStream inputStream = new FileInputStream(zipFile) {
                @Override
                public void close() throws IOException {
                    super.close();
                    Files.delete(zipFile.toPath());
                }
            };
            InputStreamResource resource = new InputStreamResource(inputStream);
            return ResponseEntity.ok().contentType(MediaType.APPLICATION_OCTET_STREAM).header(HttpHeaders.CONTENT_DISPOSITION,
                    "attachment;filename=" + URLEncoder.encode(zipFile.getName(), "UTF-8")).body(resource);
        } catch (Exception e) {
            return ResponseEntity.badRequest().body("文件不存在!");
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章