[ACCEPTED]-Create UTF-8 file using HttpServletResponse-outputstream

Accepted answer
Score: 15

You need to use the character writer of 8 the response, not the byte output stream.


ServletOutputStream os = res.getOutputStream();
os.print("Hello World");


res.getWriter().write("Some UTF-8");

Further, I'd 7 recommend setting content type to text/plain, not 6 to an overly generic one which implies binary 5 content, not character content.

I'm not 4 sure about Notepad++, but in Notepad, if 3 the text document does not contain any characters 2 beyond the ANSI range, it will be interpreted 1 as ANSI. Don't mislead you by this behaviour.

Score: 7

Here is my sample:

private static final String KALIMAH = "\u0644\u064e\u0622 \u0625\u0650\u0644\u0670\u0647\u064e \u0625\u0650\u0644\u0651\u064e\u0627 \u0627\u0644\u0644\u0647\u064f \u0645\u064f\u062d\u064e\u0645\u0651\u064e\u062f\u064c \u0631\u0651\u064e\u0633\u064f\u0648\u0652\u0644\u064f \u0627\u0644\u0644\u0647\u0650";

protected void printGreeting (HttpServletResponse res) throws IOException {
    res.setContentType( "text/html" );
    res.setCharacterEncoding( "UTF-8" );
    PrintWriter out = res.getWriter();
    out.write( KALIMAH );


More Related questions