如何将文件添加到压缩文件中以供下载?

xqkwcwgp  于 2022-10-04  发布在  Spring
关注(0)|答案(1)|浏览(213)

我的文件没有路径,因为它存储在数据库中。我从BLOB到字节检索它。每当我下载.ZIP文件时,即使.ZIP文件的大小不是0kb,该文件也是无效的和空的。

@RequestMapping(value = "/downloadMultipleFilesAsZip", method = RequestMethod.POST)
        public void downloadMultiAsZip(@RequestParam("selectedFile[]") int[] selectedFiles,
                HttpServletRequest request, HttpServletResponse response) throws FileNotFoundException, IOException {

                List<FileUpload> fileToDownload = new ArrayList<FileUpload>();
                System.out.println("==================================================");
                for (int i = 0; i<selectedFiles.length; i++) {
                    System.out.println("Value of multipleFile["+i+"] is " + selectedFiles[i]);
                    FileUpload fu = new FileUpload();
                    fu.setFile_id(selectedFiles[i]);
                    fileToDownload.add(fu);
                    System.out.println("fileToDownload file_id is " + fileToDownload.get(i).getFile_id());
                }
                // store details of downloads into fileToDownload
                fileToDownload = fuMapper.downloadMultipleAsZip(fileToDownload);

                //Do download
                OutputStream os = null;

                try {                  
                    response.setContentType("application/zip");
                    response.setHeader("Content-Disposition", "attachment; filename=download.zip");

                    os = response.getOutputStream();

                    ZipOutputStream zos = new ZipOutputStream(os);
                    System.out.println("================== Starting zip ==================");
                    for(FileUpload fu : fileToDownload) {
                        System.out.println("Zipping " + fu.getFile_name() + ".....");
                        ZipEntry zipEntry = new ZipEntry(fu.getFile_name());

                        os.write(fu.getFile_data());
                        zos.finish();
                        zos.putNextEntry(zipEntry);
                        zos.closeEntry();

                        } 
                    System.out.println("================== Finish zipping. ==================");

                    os.flush();
                    zos.close();
                    os.close();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

        }

每当我打开压缩文件时,都会出现以下错误

2sbarzqh

2sbarzqh1#

循环中的流是:

1.putNextEntry
1.write(是zos,不是os!)
1.closeEntry

在循环之后,您需要在zos上调用closefinish。否则,ZIP文件将不会正确结束。因为它的底层流不是您的(容器拥有它),所以我将使用finish

ZipOutputStream zos = new ZipOutputStream(os);
System.out.println("================== Starting zip ==================");
for(FileUpload fu : fileToDownload) {
    System.out.println("Zipping " + fu.getFile_name() + ".....");
    ZipEntry zipEntry = new ZipEntry(fu.getFile_name());

    zos.putNextEntry(zipEntry);
    zos.write(fu.getFile_data());
    zos.closeEntry();
}
zos.finish();

System.out.println("================== Finish zipping. ==================");

// Don't close zos or os; flushing shouldn't be necessary but won't hurt

相关问题