简体   繁体   English

Java-Servlet生成的CSV:编码始终为“未定义”

[英]CSV generated by Java-Servlet: Encoding always “undefined”

I am creating a CSV-File in my Java-Servlet. 我在Java-Servlet中创建一个CSV文件。

I am setting everything to UTF-8: 我将所有内容都设置为UTF-8:

response.setContentType("text/csv;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-Disposition", "attachment; filename=\"shippingInfosForDHLImport.csv\"");

If I access my servlet in the browser and download the file, special cases are broken and if I open the file in sublime and type: "view.encoding()" to the console it shows me: 如果我在浏览器中访问我的servlet并下载文件,则特殊情况会中断,并且如果我以崇高的态度打开文件并在控制台中输入“ view.encoding()”,则会显示以下信息:

"u'Undefined'"

The servlet is running on google app engine and I am downloading the file to a Mac. 该Servlet在Google App Engine上运行,我正在将该文件下载到Mac。

If I run the servlet locally. 如果我在本地运行servlet。 Everything works fine and the encoding in sublime is shown as UTF-8 (even if I remove the UTF-8 settings in my code). 一切正常,并且sublime中的编码显示为UTF-8(即使我在代码中删除了UTF-8设置)。

How can I get the encoding for the file right, if the code is running on App engine? 如果代码在App Engine上运行,如何正确获得文件的编码? Is there another config param I can set? 我可以设置另一个配置参数吗?

I solved the Problem by writing my CSV content to the PrintWriter: 我通过将CSV内容写入PrintWriter解决了问题:

response.getWriter().write(result);

instead of to the OutputStream: 而不是输出流:

response.getOutputStream().write(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM