【问题标题】:Encoding issue with Apache POIApache POI 的编码问题
【发布时间】:2014-07-04 06:49:44
【问题描述】:

我正在使用 Apache POI 创建 MS Excel 文件,当我在本地主机上使用它时一切正常。但是当我在 Google App Engine 上部署项目然后尝试在 MS Excel 中打开创建的文件时,我可以注意到我所有的特殊字符都变成了问号“?”。你们有谁可以告诉我为什么它可以在 localhost 上运行但在部署后无法显示特殊字符。

public void doPost(HttpServletRequest req, HttpServletResponse res)
            throws ServletException, IOException {
        try {
            OutputStream out = null;
            try
            {
                String dataa = req.getParameter("dataa");
                String json = URLDecoder.decode(dataa, "UTF-8");
                Gson gson = new Gson();
                ExcelData excelData = gson.fromJson(json, ExcelData.class);

                HSSFWorkbook workbook = new HSSFWorkbook();
                HSSFSheet sheet1 = (HSSFSheet) workbook.createSheet("myData");

                // workbook creation...

                ByteArrayOutputStream outByteStream = new ByteArrayOutputStream();
                workbook.write(outByteStream);
                byte [] outArray = outByteStream.toByteArray();
                res.setContentType("application/ms-excel");
                res.setContentLength(outArray.length);
                res.setHeader("Expires:", "0");
                res.setHeader("Content-Disposition", "attachment; filename=raport.xls");

                out = res.getOutputStream();
                out.write(outArray);
                out.flush();

            } catch (Exception e)
            {
                throw new ServletException("Exception in Excel Sample Servlet", e);
            } finally
            {
                if (out != null)
                    out.close();
            }
        } catch (Exception ex) {
            throw new ServletException(ex);
        }
    }

【问题讨论】:

  • 我怀疑问题可能出在您的代码中...您没有向我们展示。

标签: java excel google-app-engine apache-poi


【解决方案1】:

问题已解决

当您尝试调试问题一段时间然后在 stackoverflow 上发布后找到答案时,这很有趣:) 无论如何问题就在这里:

String data = URLEncoder.encode(someString);
String data2 = URLDecoder.decode(data, "UTF-8");

因此,在使用 App Engine 本地服务器数据 == data2 处理 localhost 时,但在生产服务器数据 != data2 上工作时,区别在于编码为问号的特殊字符。

// solution
String data = URLEncoder.encode(someString, "UTF-8");
String data2 = URLDecoder.decode(data, "UTF-8");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多