我正在开发一个接受 zipfile 并将解压缩并显示 csv 文件内容的 servlet。
到目前为止,我能够显示一些记录。但是,如下图所示,其中一条记录显示“问号”/无法识别的字符。
我检查了 csv 文件,它非常好。我还尝试删除文本并键入其他文本,但仍然不成功。
问题图片:
https://dl.dropbox.com/u/11910420/Screen%20Shot%202012-09-07%20at%203.18.46%20PM.png
公共类 AdminBootStrap 扩展 HttpServlet {
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws IOException {
resp.setContentType("text/plain");
PrintWriter out = resp.getWriter();
try {
ServletFileUpload upload = new ServletFileUpload();
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.
//
byte[] buf = new byte[5000];
int len;
String s = null;
while ((entry = zis.getNextEntry()) != null) {
out.println("Unzipping: " + entry.getName());
if (entry.getName().equalsIgnoreCase("booking.csv")) {
while ((len = zis.read(buf)) > 0) {
s = new String(buf);
String[] arrStr = s.split("\\n");
for (String a : arrStr) {
out.println(a);
}// end for
}
}
有任何想法吗?