I have a problem setting the charset for a CSV file I am creating on my web application (testing on windows laptop on a Tomcat9 server). This is what I do:
byte[] data = result.toString().getBytes();
ByteArrayOutputStream outStream = new ByteArrayOutputStream(data.length);
outStream.write(data, 0, data.length);
response.setContentType("text/csv;charset=UTF-8");
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setHeader("Content-Disposition", "attachment; filename=\"AEP_report.csv\"");
response.setContentLength(outStream.size());
ServletOutputStream responseOutStream = response.getOutputStream();
outStream.writeTo(responseOutStream);
outStream.flush();
outStream.close();
responseOutStream.flush();
responseOutStream.close();
I have tried to change the encoding to windows-1252
and others. For sure I have no problem generating the file but when afterwards I go to notepad++ and I change the encoding in order to see if it worked I always get the same result.
For ANSI
everything looks good but when I change to UTF-8 this is what it shows:
when it should show "Cürey".
So apparently the file is always created on ANSI.
Then it comes my real problem but it is basically the same as before. On my Quality system (on a Linux server) it is similar, the code does not affect the encoding, but apparently there the file is always created with a UTF-8 encoding. I guess this is depending on the operative system. So I guess my error is on defining the content-type. Did I do any mistake on the definition?
Thanks in advance for your help.
So after performing some test I realised that the problem is, that for such a content (String
that is generated as a ByteArrayOutputStream
) the moment where the encoding is defined is when you do this conversion.
So just add the encoding as a parameter on the method on the first line of code.
result.toString().getBytes("UTF-8");
then you do not need to define the charset again on
response.setContentType("text/csv")