[英]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.