Java 如何在SpringMVC中将字节数组转换为ZipoutStream?

Java 如何在SpringMVC中将字节数组转换为ZipoutStream?,java,spring,spring-mvc,zipoutputstream,zipinputstream,Java,Spring,Spring Mvc,Zipoutputstream,Zipinputstream,试图读取作为字节数组存储在数据库中的zip文件 .zip正在使用以下代码下载,但zip中包含的文件大小为“无”。没有数据 我已经看过很多答案,但不确定下面的代码有什么问题 请帮忙 @RequestMapping(value = ApplicationConstants.ServiceURLS.TRANSLATIONS + "/{resourceId}/attachments", produces = "application/zip") public void attachments(H

试图读取作为字节数组存储在数据库中的zip文件

.zip正在使用以下代码下载,但zip中包含的文件大小为“无”。没有数据

我已经看过很多答案,但不确定下面的代码有什么问题

请帮忙

@RequestMapping(value = ApplicationConstants.ServiceURLS.TRANSLATIONS + "/{resourceId}/attachments", produces = "application/zip")
    public void attachments(HttpServletResponse response, @PathVariable("resourceId") Long resourceId) throws IOException {

        TtTranslationCollection tr = translationManagementDAO.getTranslationCollection(resourceId);
        byte[] fileData = tr.getFile();

        // setting headers
        response.setStatus(HttpServletResponse.SC_OK);
        response.addHeader("Content-Disposition", "attachment; filename=\"attachements.zip\"");

        ZipOutputStream zipOutputStream = new ZipOutputStream(response.getOutputStream());

        ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(fileData));
        ZipEntry ent = null;
        while ((ent = zipStream.getNextEntry()) != null) {
            zipOutputStream.putNextEntry(ent);
        }
        zipStream.close();
        zipOutputStream.close();
    }

您还必须将zip文件的字节数据(内容)复制到输出

这应该有效(未经测试):

顺便问一下:为什么你不直接转发原始的zip字节内容呢

try (InputStream is = new ByteArrayInputStream(fileData));) {
    IOUtils.copy(is, response.getOutputStream());
}
或者更好(感谢@M.Deinum的评论)


您还必须将zip文件的字节数据(内容)复制到输出

这应该有效(未经测试):

顺便问一下:为什么你不直接转发原始的zip字节内容呢

try (InputStream is = new ByteArrayInputStream(fileData));) {
    IOUtils.copy(is, response.getOutputStream());
}
或者更好(感谢@M.Deinum的评论)


查看此链接:您已经有一个zip。直接将
字节[]
写入
输出流
。不需要拉链。@M.Deinum,谢谢你,它成功了。我以前没有使用过此文件程序。请查看此链接:您已经有一个zip。直接将
字节[]
写入
输出流
。不需要拉链。@M.Deinum,谢谢你,它成功了。我以前没有使用过这个文件程序。谢谢。是的,它的zip可以直接通过输出流发送。谢谢。是的,它的zip可以直接通过输出流发送。
IOUtils.copy(fileData, response.getOutputStream());