首页 > 解决方案 > 我正在尝试从 springboot 控制器下载一个 zip 文件,但我得到了奇怪的响应。请协助

问题描述

这是我的代码和代码下方的响应。

@GetMapping(value = "/download/tutorial/{tutorialId}", produces = "application/zip")
    public void zipDownload(@PathVariable String tutorialId, HttpServletResponse response) throws IOException {
        ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
        List<TutorialFileDownload> downloads = tutorialFileService.downloadFiles(compos.getLong(tutorialId));
        for (TutorialFileDownload f : downloads) {
            FileSystemResource resource = new FileSystemResource(dropBoxService.downloadFile(f.getFilename(), f.getPathLower()));
            ZipEntry zipEntry = new ZipEntry(resource.getFilename());
            zipEntry.setSize(resource.contentLength());
            zipOut.putNextEntry(zipEntry);
            StreamUtils.copy(resource.getInputStream(), zipOut);
            zipOut.closeEntry();
        }
        zipOut.finish();
        zipOut.close();
       // response.setStatus(HttpServletResponse.SC_OK);
        response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=test.zip");
    }

这是回应

回复

标签: java

解决方案


推荐阅读