0

我正在开发一个接受 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

                        }

                    }

有任何想法吗?

4

1 回答 1

1

罪魁祸首是s = new String(buf)因为它通过默认编码将一串字节解码为一串字符。不幸的是,GAE 上的默认编码是US-ASCII.

您应该对 CSV 进行编码。例如UTF-8使用:

s = new String(buf, "UTF-8");
于 2012-09-07T08:06:27.793 回答