How to set up a response header in JAX-RS so that Chinese characters display correctly in a custom generated file

How do I correctly create a "javax.ws.rs.core.Response" (which will be returned) that supports the Chinese character encoding in the Excel file?

To clarify, I have a file (CSV excel) that contains some kind of Chinese content and I need to return a javax response, which will then display the Chinese characters correctly in the document (client side).

I am currently doing the following:

return Response.status( 200 )
        .header( "content-disposition", 
                 "attachment;filename=SampleCSV.csv;charset=Unicode" )
        .entity( result )
        .build();

      

but when this response is generated and returned to the client side (and a popup is displayed asking to download the file) the Chinese content in the excel file is tapestry.

Any suggestion would be much appreciated.

+3


source to share


2 answers


RFC that defines the content header does not mention the charset clause

Try adding the correct content-type header to your answer:



.header("Content-Type", "text/csv; charset=utf-8")

      

Make sure to use utf-8 and not unicode. If that works, you can remove the sentence charset

from the content header.

+4


source


You indicate charset=Unicode

which is not valid as Unicode is not a single encoding. It is a character set with a family of encodings. UTF-8 and UTF-16 are commonly used encodings.

You can control the response header to influence how the browser / client interprets the response using annotation @Produces

. I've seen different opinions on whether this works:

I'm pretty sure this only changes the encoding declared in the response headers; it doesn't change the encoding it actually used to convert the response string to bytes for sending over the network. The two must match, otherwise the browser / client will misinterpret the response because it thinks you used a different encoding than you actually did.

If you are returning a java.lang.String object, JAx-RS uses the default encoding to convert to a byte stream. If the JAX-RS server is running on Unix, it's UTF-8, which usually works well, but on Windows it's strange that it doesn't.



Therefore, you have to force it to use a specific encoding by wrapping the result object in OutputStreamWriter

that specifies the encoding. This prevents JAX-RS from using the default transform.

To be specific, if result

is a java.lang.String object in your code, you may need to create an OutputStreamWriter around it that specifies an encoding such as UTF-8 to affect the byte stream that JAX-RS writes to the network. I haven't tested this code, but it might work:

.entity(new OutputStreamWriter(result, "UTF-8"))

      

I had a problem with Tika submitting StreamingOutput

instead Response

and building it by default OutputStreamWriter

, which uses the system default encoding instead of the predictable one.

I changed Tika to indicate the encoding when building OutputStreamWriter

, and added annotation charset

to @Produces

and that fixed that for me.

+2


source







All Articles