如何为“虚拟文件”列表创建 ZIP 文件并输出到httpservletresponse

发布于 2024-10-11 22:01:51 字数 279 浏览 4 评论 0原文

我的目标是将多个 java.io.File 对象放入一个 zip 文件中,并打印到 HttpServletResponse 供用户下载。

这些文件是由 JAXB 编组器创建的。它是一个 java.io.File 对象,但它实际上并不在文件系统上(它仅在内存中),所以我无法创建 FileInputStream。

我见过的所有资源都使用 OutputStream 来打印 zip 文件内容。但是,所有这些资源都使用 FileInputStream (我无法使用)。

有人知道我怎样才能做到这一点吗?

My goal is to put multiple java.io.File objects into a zip file and print to HttpServletResponse for the user to download.

The files were created by the JAXB marshaller. It's a java.io.File object, but it's not actually on the file system (it's only in memory), so I can't create a FileInputStream.

All resources I've seen use the OutputStream to print zip file contents. But, all those resources use FileInputStream (which I can't use).

Anyone know how I can accomplish this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

み零 2024-10-18 22:01:51

查看 Apache Commons Compress 库,它提供了您需要的功能。

当然,“埃里克森”对你的问题的评论是正确的。您将需要文件内容而不是 java.io.File 对象。在我的例子中,我假设你有一个方法
byte[] getTheContentFormSomewhere(int fileNummer) 返回第 fileNummer 文件的文件内容(在内存中)。 -- 当然,这个功能的设计很糟糕,但这只是为了说明。

它应该有点像这样工作:

void compress(final OutputStream out) {
  ZipOutputStream zipOutputStream = new ZipOutputStream(out);
  zipOutputStream.setLevel(ZipOutputStream.STORED);

  for(int i = 0; i < 10; i++) {
     //of course you need the file content of the i-th file
     byte[] oneFileContent = getTheContentFormSomewhere(i);
     addOneFileToZipArchive(zipOutputStream, "file"+i+"."txt", oneFileContent);
  }

  zipOutputStream.close();
}

void addOneFileToZipArchive(final ZipOutputStream zipStream,
          String fileName,
          byte[] content) {
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(fileName);
    zipStream.putNextEntry(zipEntry);
    zipStream.write(pdfBytes);
    zipStream.closeEntry();
}

http 控制器的片段:

HttpServletResponse response
...
  response.setContentType("application/zip");
  response.addHeader("Content-Disposition", "attachment; filename=\"compress.zip\"");
  response.addHeader("Content-Transfer-Encoding", "binary");
  ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream();
  compress(outputBuffer);
  response.getOutputStream().write(outputBuffer.toByteArray());
  response.getOutputStream().flush();
  outputBuffer.close();

Have a look at the Apache Commons Compress library, it provides the functionality you need.

Of course "erickson" is right with his comment to your question. You will need the file content and not the java.io.File object. In my example I assume that you have a method
byte[] getTheContentFormSomewhere(int fileNummer) which returns the file content (in memory) for the fileNummer-th file. -- Of course this function is poor design, but it is only for illustration.

It should work a bit like this:

void compress(final OutputStream out) {
  ZipOutputStream zipOutputStream = new ZipOutputStream(out);
  zipOutputStream.setLevel(ZipOutputStream.STORED);

  for(int i = 0; i < 10; i++) {
     //of course you need the file content of the i-th file
     byte[] oneFileContent = getTheContentFormSomewhere(i);
     addOneFileToZipArchive(zipOutputStream, "file"+i+"."txt", oneFileContent);
  }

  zipOutputStream.close();
}

void addOneFileToZipArchive(final ZipOutputStream zipStream,
          String fileName,
          byte[] content) {
    ZipArchiveEntry zipEntry = new ZipArchiveEntry(fileName);
    zipStream.putNextEntry(zipEntry);
    zipStream.write(pdfBytes);
    zipStream.closeEntry();
}

Snipets of your http controller:

HttpServletResponse response
...
  response.setContentType("application/zip");
  response.addHeader("Content-Disposition", "attachment; filename=\"compress.zip\"");
  response.addHeader("Content-Transfer-Encoding", "binary");
  ByteArrayOutputStream outputBuffer = new ByteArrayOutputStream();
  compress(outputBuffer);
  response.getOutputStream().write(outputBuffer.toByteArray());
  response.getOutputStream().flush();
  outputBuffer.close();
岁月静好 2024-10-18 22:01:51

事实证明我是个白痴:)正在“创建”的文件保存到无效路径并吞下异常,所以我认为它正在“创建”正常。然而,当我尝试实例化一个新的 FileInputStream 时,它抱怨文件不存在(确实如此)。我脑子一热,假设 java.io.File 对象实际上在某个地方包含文件信息。但正如埃里克森指出的那样,这是错误的。

感谢拉尔夫提供的代码,我在解决无效路径问题后使用了它。

我的代码:

ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
byte[] buf = new byte[1024];

File file;
InputStream in;
// Loop through entities
for (TitleProductAccountApproval tpAccountApproval : tpAccountApprovals) {
    // Generate the file    
    file = xmlManager.getXML(
        tpAccountApproval.getTitleProduct().getTitleProductId(), 
        tpAccountApproval.getAccount().getAccountId(), 
        username);

    // Write to zip file
    in = new FileInputStream(file);
    out.putNextEntry(new ZipEntry(file.getName()));

    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }

    out.closeEntry();
    in.close();
}

out.close();

Turns out I'm an idiot :) The file that was being "created" was saving to invalid path and swallowing the exception, so I thought it was being "created" ok. When I tried to to instantiate a new FileInputStream, however, it complained that file didn't exist (rightly so). I had a brainfart and assumed that the java.io.File object actually contained file information in it somewhere. But as erickson pointed out, that was false.

Thanks Ralph for the code, I used it after I solved the invalid pathing issue.

My code:

ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
byte[] buf = new byte[1024];

File file;
InputStream in;
// Loop through entities
for (TitleProductAccountApproval tpAccountApproval : tpAccountApprovals) {
    // Generate the file    
    file = xmlManager.getXML(
        tpAccountApproval.getTitleProduct().getTitleProductId(), 
        tpAccountApproval.getAccount().getAccountId(), 
        username);

    // Write to zip file
    in = new FileInputStream(file);
    out.putNextEntry(new ZipEntry(file.getName()));

    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }

    out.closeEntry();
    in.close();
}

out.close();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文