如何将 POST 数据从 servlet 读取到 ZipStream 中?
情况是 ZIP 文件已发布到 Tomcat 服务器,并且由于它没有与之关联的参数名称,因此我们将直接转到请求的流。
ServletInputStream sis = request.getInputStream()
ZipInputStream zis = new ZipInputStream(sis)
ZipEntry zEntry = zis.getNextEntry()
while (zEntry != null) {
// do something with zEntry
zEntry = zis.getNextEntry()
}
非常简单,但行不通。它永远不会进入 while 循环,因为第一个 zEntry 为 null。 (顺便说一句,ZIP 文件是有效的)
有什么想法吗? 谢谢
编辑:类型是multipart/form-data(“multipart/form-data;boundary=--------------------8ce556d90e9deb6”)
The situation is that a ZIP file has been POSTed to a Tomcat server and since it has no parameter name associated with it, we're going right to the request's stream.
ServletInputStream sis = request.getInputStream()
ZipInputStream zis = new ZipInputStream(sis)
ZipEntry zEntry = zis.getNextEntry()
while (zEntry != null) {
// do something with zEntry
zEntry = zis.getNextEntry()
}
Compellingly simple, but it doesn't work. It never enters the while loop because the first zEntry is null. (The ZIP file is valid, btw)
Any ideas?
Thanks
EDIT: Type is multipart/form-data ("multipart/form-data; boundary=---------------------8ce556d90e9deb6")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用
multipart/form-data
解析器来提取上传的文件。您不应该将其原始数据提供给ZipInputStream
。然而令我惊讶的是,它并没有抛出它不是 ZIP 文件格式的异常。getParameter()
和 consorts 仅针对application/www-form-urlencoded
请求而设计。它们都会在multipart/form-data
请求上返回null
。您需要使用getParts()
方法代替。或者,当您仍在使用 Servlet 2.5 或更早版本时,您需要在 Apache Commons Fileupload 的帮助下解析正文。无论哪种方式,您都应该能够迭代
multipart/form-data
项,并以InputStream
形式获取上传的文件,然后使用读取该文件ZipInputStream。
另请参阅:
You need to use a
multipart/form-data
parser to extract the uploaded file. You shouldn't feed it raw to theZipInputStream
. It however surprises me that it didn't threw an exception that it's not in ZIP file format.The
getParameter()
and consorts are designed forapplication/www-form-urlencoded
requests only. They will all returnnull
onmultipart/form-data
requests. You need to usegetParts()
method instead. Or, when you're still on Servlet 2.5 or older, then you need to parse the body with help of Apache Commons Fileupload.Either way, you should be able to iterate over
multipart/form-data
items and get the uploaded file(s) as anInputStream
which you in turn read usingZipInputStream
.See also: