0

Requirements

Downlading a CSV File

Code

I have a csvFormattedString like

String csvFormattedString = "\"Column_One\",\"Column_Two\"\n\"Row_Col1\",\"Row_Col2\"\n";

This CSV String is written to the reponse print writer using

response.getWriter().write(csvFormattedString);

I have set the headers as application-force-download and have set the charcter encoding to UTF-8.

I would like to send the response length back to the user as well.

The csvFormattedString.length() does not seem to be correct as some my characters get truncated

Anand Sunderraman
  • 7,900
  • 31
  • 90
  • 150

1 Answers1

1

csvFormattedString.length() counts the characters. Use s.getBytes("UTF-8").length to get the number of bytes used for that string represented as UTF-8.

You have to catch UnsupportedEncodingException in order to use getBytes(String encoding).

vanje
  • 10,180
  • 2
  • 31
  • 47