[英]unzip a specific file in the zip file uploaded by sp using java and google app engine
我试图找出使用Java和Google App Engine解压缩特定文件的解决方案。 我尝试使用ZipInputStream,但无法访问在jsp中上载的zip文件。 有人可以帮助我摆脱困境吗?
ServletFileUpload upload = new ServletFileUpload();
resp.setContentType("text/plain");
FileItemIterator iterator = upload.getItemIterator(req);
while (iterator.hasNext()) {
FileItemStream fileItemStream = iterator.next();
InputStream InputStream = fileItemStream.openStream();
if (!fileItemStream.isFormField()) {
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(InputStream));
ZipEntry entry;
while ((entry = zis.getNextEntry()) != null) {
//code to access required file in the zip file
}
}
}
我的猜测是ZipInputStream
需要可搜索的流。 servlet(通常是网络)返回的流是不可搜索的。
尝试读取整个流,然后将其包装在ByteArrayInputStream
:
byte[] bytes = readBytes(fileItemStream.openStream());
InputStream bufferedStream = new ByteArrayInputStream(bytes);
public static byte[] readBytes(InputStream is) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int len;
byte[] data = new byte[100000];
while ((len = is.read(data, 0, data.length)) != -1) {
buffer.write(data, 0, len);
}
buffer.flush();
return buffer.toByteArray();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.