0

I'm using openCSV to generate CSV file and OutputStreamWriter to attach that file to http response.

The biggest issue here is that for example '€' in downloaded CSV file is '€'. Obviously the encoding is wrong but I've set it to 'UTF-8'. Additionally I've set the encoding in pom.xml to 'UTF-8'.

response.setHeader("Content-Disposition", "attachment; filename=" + filename + ".csv");
response.setContentType(BulkListExportConstants.CSV_CONTENT_TYPE);
OutputStreamWriter osw = new OutputStreamWriter(response.getOutputStream(), "UTF-8");

List<String[]> result = iSmsExportService.csvExport(columnNames);
CSVWriter csvWriter = new CSVWriter(osw, BulkListSharedConstants.CSV_SEPARATOR);
csvWriter.writeAll(result);
csvWriter.flush();
csvWriter.close();
osw.flush();
osw.close();

Any suggestions?

Wajdy Essam
  • 4,280
  • 3
  • 28
  • 33
Reeebuuk
  • 1,363
  • 2
  • 21
  • 42
  • 1
    First find out where it goes wrong: is the file sent over the line correct and the receiver interprets it wrongly or is the sender already sending something wrong? – Henry Jun 18 '14 at 07:55
  • 3
    You are encoding the document correctly; you are reading it incorrectly as ISO-8859-1. – McDowell Jun 18 '14 at 08:57
  • 1
    if you read the documnet by any microsoft product (excel for example) you need to set BOM on the file (prepending EF BB BF) on the first byte. – Wajdy Essam Jun 18 '14 at 09:06
  • I've added UTF8_BOM = "\uFEFF"; to the osw and it worked like a charm. Can you write an answer below? :) – Reeebuuk Jun 18 '14 at 09:41

1 Answers1

0

as McDowell said, your encoding is done correctly, but if you want to read this csv from any BOM aware products (like Excel for example) you need to insert the BOM into the beginning of the file/stream

So you could change the response:

OutputStream os = response.getOutputStream();
os.write(239); //0xEF
os.write(187); //0xBB
os.write(191); //0xBF

// then write your data normally
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");

This will insert the BOM into the beginning of the file.

Montag451
  • 1,168
  • 3
  • 14
  • 30
Wajdy Essam
  • 4,280
  • 3
  • 28
  • 33