在servlet上显示分隔文本时无法识别的字符

时间:2012-09-07 07:32:15

标签: google-app-engine servlets csv delimited-text

我正在研究一个接受zipfile的servlet,并将解压缩并显示csv文件的内容。

到目前为止,我能够显示一些记录。但是,如下图所示,其中一条记录是覆盖“问号”/无法识别的字符。

我检查了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

                        }

                    }

任何想法?

1 个答案:

答案 0 :(得分:1)

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

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

s = new String(buf, "UTF-8");