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”)
You need to use a
multipart/form-dataparser 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-urlencodedrequests only. They will all returnnullonmultipart/form-datarequests. 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-dataitems and get the uploaded file(s) as anInputStreamwhich you in turn read usingZipInputStream.See also: