使用Apache POI编码问题

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

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); } } 

问题解决了

当你尝试debugging一段时间的问题,然后find刚刚发布在stackoverflow后的答案是相当有趣:)无论如何问题在这里:

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

因此,在使用App Engine本地服务器数据== data2的情况下使用本地主机时,在生产服务器数据上使用!= data2时,差别是编码为问号的特殊字符。

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