从服务器下载的压缩文件无法打开。

huangapple go评论77阅读模式
英文:

the zip file download from server could not be opened

问题

I am using this java(version 11) function to download zip file from server side in Spring Boot (v2.6.7):

public void downloadPhotoBatchTest() throws IOException {
        ZipFile zipFile = new ZipFile(new File("/Users/john/59c49360-3b55-4f97-910e-9f33907f10cb.zip"));
        httpResponse.setContentType("application/zip");
        httpResponse.setHeader("Content-Disposition", "attachment; filename=\"" + zipFile.getName() + "\"");
        httpResponse.setHeader("Content-Length", String.valueOf(zipFile.size()));
        try (OutputStream out = httpResponse.getOutputStream();
             FileInputStream in = new FileInputStream("/Users/john/59c49360-3b55-4f97-910e-9f33907f10cb.zip")) {
            byte[] buffer = new byte[4096];
            int length;
            while ((length = in.read(buffer)) > 0) {
                out.write(buffer, 0, length);
            }
            out.flush();
        } catch (IOException e) {
            log.error("write zip to response error", e);
        }
    }

but now I found the zip file size will not the same with server side, the downloaded file only have 2KB, and the downloaded file could not be opened. Am I missing something with this code, I read the code but could not found the point where cause this issue. what should I do to fixed this problem? This is the httpResponse define:

@Autowired
private HttpServletResponse httpResponse;
英文:

I am using this java(version 11) function to download zip file from server side in Spring Boot (v2.6.7):

public void downloadPhotoBatchTest() throws IOException {
        ZipFile zipFile = new ZipFile(new File("/Users/john/59c49360-3b55-4f97-910e-9f33907f10cb.zip"));
        httpResponse.setContentType("application/zip");
        httpResponse.setHeader("Content-Disposition", "attachment; filename=\"" + zipFile.getName() + "\"");
        httpResponse.setHeader("Content-Length", String.valueOf(zipFile.size()));
        try (OutputStream out = httpResponse.getOutputStream();
             FileInputStream in = new FileInputStream("/Users/john/59c49360-3b55-4f97-910e-9f33907f10cb.zip")) {
            byte[] buffer = new byte[4096];
            int length;
            while ((length = in.read(buffer)) > 0) {
                out.write(buffer, 0, length);
            }
            out.flush();
        } catch (IOException e) {
            log.error("write zip to response error", e);
        }
    }

but now I found the zip file size will not the same with server side, the downloaded file only have 2KB, and the downloaded file could not be opened. Am I missing something with this code, I read the code but could not found the point where cause this issue. what should I do to fixed this problem? This is the httpResponse define:

@Autowired
private HttpServletResponse httpResponse;

答案1

得分: 2

除了内容类型之外,您的代码不需要关心是zip文件还是其他文件类型。它只需要传输文件。

特别注意,不要使用ZipFile.size()作为Content-Length,因为ZipFile.size()返回zip文件中的文件数量,而Content-Length需要的是字节数。

您没有指定httpResponse是什么以及它来自哪里。而且看起来不像是典型的Spring Boot 写法。更好的Spring Boot 代码应该是:

@RequestMapping("/photoBatch")
public ResponseEntity<InputStreamResource> downloadPhotoBatch() throws IOException {

    File file = new File("/Users/john/59c49360-3b55-4f97-910e-9f33907f10cb.zip");
    InputStreamResource resource = new InputStreamResource(new FileInputStream(file));

    return ResponseEntity.ok()
            .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + file.getName())
            .contentType(MediaType.parseMediaType("application/zip"))
            .contentLength(file.length())
            .body(resource);
}

如果您有其他需要翻译的内容,请告诉我。

英文:

Except for the content type, your code doesn't need to care if it is a zip file or some other file type. It just needs to transfer the file.

In particular, do not use ZipFile.size() for Content-Length as ZipFile.size() returns the number of files within the zip file while Content-Length expects the number of bytes.

You don't specify what httpResponse is and where it comes from. And it doesn't look like idiomatic Spring Boot. Better Spring Boot code would be:

@RequestMapping(&quot;/photoBatch&quot;)
public ResponseEntity&lt;InputStreamResource&gt; downloadPhotoBatch() throws IOException {

    File file = new File(&quot;/Users/john/59c49360-3b55-4f97-910e-9f33907f10cb.zip&quot;);
    InputStreamResource resource = new InputStreamResource(new FileInputStream(file));

    return ResponseEntity.ok()
            .header(HttpHeaders.CONTENT_DISPOSITION, &quot;attachment;filename=&quot; + file.getName())
            .contentType(MediaType.parseMediaType(&quot;application/zip&quot;)
            .contentLength(file.length())
            .body(resource);
}

huangapple
  • 本文由 发表于 2023年5月15日 03:09:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249259.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定