如何在Spring MVC中将字节数组转换为ZipOutputStream?

Amol Patil:

尝试读取以字节数组形式存储在数据库中的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文件的字节数据(内容)也复制到输出中...

这应该工作(未试用):

while ((ent = zipStream.getNextEntry()) != null) {
    zipOutputStream.putNextEntry(ent);
    // copy byte stream
    org.apache.commons.io.IOUtils.copy(zis.getInputStream(ent), zipOutputStream);
}

顺便说一句:为什么您不仅仅转发原始的zip字节内容?

try (InputStream is = new ByteArrayInputStream(fileData));) {
    IOUtils.copy(is, response.getOutputStream());
}

甚至更好(感谢@M。Deinum的评论)

IOUtils.copy(fileData, response.getOutputStream());

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在Android中将位图转换为字节数组并将字节数组转换为位图?

如何在Swift中将Int转换为4个字节的字节数组?

如何在Go中将字节数组转换为字符串数组?

如何在C ++中将浮点数组转换为字节数组(Arduino)

如何在c#中将浮点数组转换为字节数组?

如何在C ++中将64位整数转换为大端字节数组

如何在Java中将字节数组转换为十六进制字符串?

如何在Java中将字符串转换为bcd格式的字节数组?

如何在m子中将字节数组转换为多部分文件

如何在Rust中将字节数组转换为原始类型?

如何在 TwinCAT3 中将字节数组转换为各种 LREAL(double) 值

如何在PowerShell中将哈希字符串转换为字节数组?

如何在Android Studio中将imageview转换为字节数组?

如何在Python中将字节数组值转换为float

如何在Java中将图像转换为字节数组?

如何在Java中将字节数组转换为Mat对象

如何在ObjectiveC中将字节数组转换为浮点值?

如何在Java SE中将字节数组转换为Image

如何在C#中将字节数组的子范围转换为字符串

如何在go中将int64转换为字节数组?

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

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

如何在Perl中将图像或文本文件转换为字节数组?

如何在php中将字符串转换为ascii字节数组

如何在Java中将整数值转换为'n'位数字ascii字节数组

如何在Kotlin中将对象转换为字节数组

如何在类似于Java的Perl中将bigint转换为字节数组

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

如何在Java中将带符号的字节数组转换为ASCII