How do you read POST data from a servlet into a ZipStream?
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 someth开发者_如何学JAVAing 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")
EDIT: Type is multipart/form-data ("multipart/form-data; boundary=---------------------8ce556d90e9deb6")
You need to use a multipart/form-data
parser to extract the uploaded file. You shouldn't feed it raw to the ZipInputStream
. 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 for application/www-form-urlencoded
requests only. They will all return null
on multipart/form-data
requests. You need to use getParts()
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 an InputStream
which you in turn read using ZipInputStream
.
See also:
- How to upload files to server using JSP/Servlet?
精彩评论