【发布时间】:2012-08-31 14:18:33
【问题描述】:
我正在尝试创建一个 servlet,它能够解压缩包含 3 个 csv 文件的文件夹,然后打印出每个 csv 文件的数据。
我一直在尝试使用 ZipInputStream,但它没有为我提供读取/打印每个 csv 内容的能力。
当我在 GAE 上构建这个网络应用时,我无法使用 FileOutputStream。
有没有办法使用 ZipInputStream 解压缩和读取单个 csv 而无需在 GAE 上创建 csv?
公共类 AdminBootStrap 扩展 HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
PrintWriter out = resp.getWriter();
try {
ServletFileUpload upload = new ServletFileUpload();
resp.setContentType("text/plain");
FileItemIterator iterator = upload.getItemIterator(req);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
InputStream in = item.openStream();
if (item.isFormField()) {
out.println("Got a form field: " + item.getFieldName());
} else {
out.println("Got an uploaded file: " + item.getFieldName() +
", name = " + item.getName());
ZipInputStream zis = new ZipInputStream(new BufferedInputStream(in));
ZipEntry entry;
// Read each entry from the ZipInputStream until no
// more entry found indicated by a null return value
// of the getNextEntry() method.
//
while ((entry = zis.getNextEntry()) != null) {
out.println("Unzipping: " + entry.getName());
//until this point, i'm only available to print each csv name.
//What I want to do is to print out the data inside each csv file.
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
// throw new ServletException(ex);
}
}
}
【问题讨论】:
-
我知道我可以使用以下命令打印出数据:System.out.write(buf, 0, len);但是是否可以将这些数据直接存储到 String 变量中?
标签: google-app-engine servlets google-cloud-datastore multifile-uploader zipinputstream