如何为“虚拟文件”列表创建 ZIP 文件并输出到httpservletresponse
我的目标是将多个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
查看 Apache Commons Compress 库,它提供了您需要的功能。
当然,“埃里克森”对你的问题的评论是正确的。您将需要文件内容而不是 java.io.File 对象。在我的例子中,我假设你有一个方法
byte[] getTheContentFormSomewhere(int fileNummer)
返回第 fileNummer 文件的文件内容(在内存中)。 -- 当然,这个功能的设计很糟糕,但这只是为了说明。它应该有点像这样工作:
http 控制器的片段:
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 methodbyte[] 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:
Snipets of your http controller:
事实证明我是个白痴:)正在“创建”的文件保存到无效路径并吞下异常,所以我认为它正在“创建”正常。然而,当我尝试实例化一个新的 FileInputStream 时,它抱怨文件不存在(确实如此)。我脑子一热,假设 java.io.File 对象实际上在某个地方包含文件信息。但正如埃里克森指出的那样,这是错误的。
感谢拉尔夫提供的代码,我在解决无效路径问题后使用了它。
我的代码:
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: