开发者

The UTF chars are junk if we export data from JSP to csv file

开发者 https://www.devze.com 2023-03-13 07:26 出处:网络
The data is coming from the Flex in utf format, but when we export to the csv file it is corrupted. if we open the same csv file in notepad++ , editplus it is showing proper chars.

The data is coming from the Flex in utf format, but when we export to the csv file it is corrupted. if we open the same csv file in notepad++ , editplus it is showing proper chars. - there are japanses and korean char.

for is the code snippet we are using. Kin开发者_运维技巧dly us know we can handle the different lang in case of export. we have written the demo application in java it is working file.

We have put the UTF8+BOM , the file is able to open properly in csv. (this is from Java code) but if we try to add the same from jsp page it is not working.

<%
    MxHtmlUtil.jspInitScript(request, response);
    response.setContentType("application/csv; charset=UTF-8"); 
    response.setHeader("content-disposition", "attachment; filename=multi-cms-search.csv");

    if (request.getParameter("content") != null)
        out.println(request.getParameter("content"));
%>

Added bom as follows but not working in jsp page , it is working fine in java code.

    out.print("\ufeff"); 
    out.println(request.getParameter("content"));


JSP is as being a view technology designed to generate and send HTML output, not other file formats. All whitespace and newlines outside the <% %> things are also sent to the output. It may have corrupted the one and other.

Remove all that whitespace from the JSP or, better, put all that Java code in a normal Java class. For that a Servlet is perfectly suitable.

Note that you should also be calling

response.setCharacterEncoding("UTF-8");

before writing any byte to the response body. It namely instructs the writer what charset to use to translate the written characters to bytes in the response body.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号