【问题标题】:unzip a specific file in the zip file uploaded by sp using java and google app engine使用java和google app engine解压sp上传的zip文件中的特定文件
【发布时间】:2014-05-06 06:04:02
【问题描述】:

我试图找出使用 java 和 Google 应用引擎解压缩特定文件的解决方案。我尝试使用 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
                      }

                  } 
              }

【问题讨论】:

  • 感谢彼得的回复。请参考上面,我已经编辑了代码

标签: java jsp google-app-engine servlets


【解决方案1】:

我的猜测是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();
}

【讨论】:

  • 我是 Java 新手。您能告诉我读取 zip 文件的过程吗?
猜你喜欢
  • 2014-07-30
  • 2014-12-28
  • 2012-08-31
  • 1970-01-01
  • 1970-01-01
  • 2020-11-30
  • 2011-02-12
  • 2010-11-01
  • 2010-09-10
相关资源
最近更新 更多