Google app engine 在谷歌应用程序引擎(Java)上解压并读取每个文件
我正在尝试创建一个servlet,它能够解压包含3个csv文件的文件夹,然后打印出每个csv文件的数据 我一直在尝试使用ZipInputStream,但它没有为我提供读取/打印每个csv内容的能力 由于我正在GAE上构建此web应用程序,因此无法使用FileOutputStream 有没有办法使用ZipInputStream解压和读取单个csv,而无需在GAE上创建csv 公共类AdminBootStrap扩展了HttpServlet{Google app engine 在谷歌应用程序引擎(Java)上解压并读取每个文件,google-app-engine,servlets,google-cloud-datastore,multifile-uploader,zipinputstream,Google App Engine,Servlets,Google Cloud Datastore,Multifile Uploader,Zipinputstream,我正在尝试创建一个servlet,它能够解压包含3个csv文件的文件夹,然后打印出每个csv文件的数据 我一直在尝试使用ZipInputStream,但它没有为我提供读取/打印每个csv内容的能力 由于我正在GAE上构建此web应用程序,因此无法使用FileOutputStream 有没有办法使用ZipInputStream解压和读取单个csv,而无需在GAE上创建csv 公共类AdminBootStrap扩展了HttpServlet{ public void doPost(HttpServle
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);
}
}
}
ZipInputStream
是一个输入流
,因此您可以正常读取:
while ((entry = zis.getNextEntry()) {
byte[] buf = new byte[1024];
int len;
while ((len = zis.read(buf)) > 0) {
// here do something with data in buf
}
我知道我可以使用:System.out.write(buf,0,len)打印数据;但是有可能将这些数据直接存储到字符串变量中吗?