Question

From the RFC for card 4.0 I learned that vcard 4.0 is always utf-8.

I am using ez-vcard to export contacts into a export.vcf file transferred via http:

response.setContentType("text/vcard; charset=utf-8");
response.setStatus(HttpServletResponse.SC_OK);
PrintWriter writer = response.getWriter();

VCardWriter vCardWriter = new VCardWriter(writer, VCardVersion.V4_0);

// add cards...

vCardWriter.close();

Guess what? Characters are not being encoded properly. If I open the file in a text editor, I see characters are messed up.

Any help?

Was it helpful?

Solution

It may be ignoring the character encoding specified in the content type because you are setting it to something other than text/html.

Try setting the character encoding using setCharacterEncoding() instead (make sure to call it before calling getWriter()).

response.setContentType("text/vcard");
response.setCharacterEncoding("UTF-8");
response.setStatus(HttpServletResponse.SC_OK);
PrintWriter writer = response.getWriter();

Also, make sure your text editor is reading the file correctly. During my testing, I found that Eclipse would not display UTF-8 characters correctly, because it was configured to load the file under a different character set. Try viewing the file contents from the terminal:

cat the-vcard-file.vcf

EDIT: One more thing: Do not close the VCardWriter object. This will close the servlet's PrintWriter object, which you must never close!!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top