Java字符串HTTP编码



我有以下方法来向浏览器套接字写入HTTP响应。

public static void writeResponse(OutputStream os, HttpResponse response)
        throws IOException {    
    String total = just my http response text;  
    os.write(total.getBytes());
    //os.write(new String(total.getBytes(ISO_8859_1), UTF_8).getBytes()); - my try
    os.flush();
}

字符集为charset=UTF-8。内容类型为text/plain。文本输出文本为

"Hello-engnПривет-rusnこんにちは-japnनमस्ते-hindnأهلا-arab"

但是我的浏览器除了英语什么都看不懂。

你好eng������-rus-jap-后-阿拉伯

怎么了?

将响应字符串转换为UTF_8字节数组。并将其写入响应流。

os.write(total.getBytes(UTF_8));

您需要设置http头中的字符集以匹配编码如何在Java 中更改HTTP响应中的字符集编码

最新更新