Java 改变 HttpServletResponse 的编码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1849058/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Change encoding of HttpServletResponse
提问by Pablo Fernandez
I have an API that returns XML, it actually returns it using the default encoding (I believe it's UTF-8), but now requirements have changed and we need to return everything in UTF-16LE.
我有一个返回 XML 的 API,它实际上使用默认编码返回它(我相信它是 UTF-8),但现在要求已经改变,我们需要以 UTF-16LE 返回所有内容。
My question is: is there an easy way of doing this? I have access to the response just before the calls complete so I was wondering if I could do something like
我的问题是:有没有一种简单的方法可以做到这一点?我可以在呼叫完成之前访问响应,所以我想知道我是否可以做类似的事情
//This method does not exist
response.setCharacterEncoding("UTF-16LE");
Thanks a lot!
非常感谢!
UPDATE: The method mentioned is the one to use. I was using an old version (2.3) of the servlet API that did not include it. Changing the version fixed it all.
更新:提到的方法是要使用的方法。我使用的是不包含它的 servlet API 的旧版本 (2.3)。更改版本解决了所有问题。
采纳答案by skaffman
Uhh, the method doesexist, here
呃,方法确实存在,在这里
Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8. If the character encoding has already been set by setContentType(java.lang.String) or setLocale(java.util.Locale), this method overrides it. Calling setContentType(java.lang.String) with the String of text/html and calling this method with the String of UTF-8 is equivalent with calling setContentType with the String of text/html; charset=UTF-8.
将发送到客户端的响应的字符编码(MIME 字符集)设置为 UTF-8。如果字符编码已由 setContentType(java.lang.String) 或 setLocale(java.util.Locale) 设置,则此方法将覆盖它。用text/html的String调用setContentType(java.lang.String),用UTF-8的String调用这个方法,等价于用text/html的String调用setContentType;字符集=UTF-8。
回答by Jonathan Feinberg
First
第一的
response.setHeader("Content-Type", "text/xml; charset=UTF-16LE");
Then, make sure you're actually emitting that encoding!
然后,确保您确实在发出该编码!
回答by Tim Cooper
As others have stated, use either:
正如其他人所说,使用:
response.setCharacterEncoding("UTF-16LE");
or:
或者:
response.setHeader("Content-Type", "text/xml; charset=UTF-16LE");
...but make sure you do this beforecalling response.getWriter(); ...!
...但请确保在调用 response.getWriter();之前执行此操作 ……!
回答by Muhammad Nuruddin
just do the following thing:
只需做以下事情:
byte[] k =xml.getBytes("UTF-16"); // xml is the string with unicode content. getBytes("UTF-16") encodes given String into a sequence of bytes and returns an array of bytes. you can use xml.getBytes(UTF8_CHARSET); for utf-8 encoding
response.setContentType("text/xml");
response.setContentLength(k.length);
response.getOutputStream().write(k);
response.getOutputStream().flush();
response.getOutputStream().close();
回答by Derek Wade
I found that you MUST set the character encoding to at least UTF-8 because the default is ISO-8859-1. The ISO-8859-1 character set doesn't account for some extended characters. I wrote a helper function to use what is sent in the "Accept" header:
我发现您必须至少将字符编码设置为 UTF-8,因为默认值为 ISO-8859-1。ISO-8859-1 字符集不考虑某些扩展字符。我编写了一个辅助函数来使用“Accept”标头中发送的内容:
public static void setResponseCharacterSet(HttpServletRequest request, HttpServletResponse response)
{
String type = "UTF-8";
if(request.getHeader("accept") != null)
{
String[] params = request.getHeader("accept").split("charset=");
if(params.length == 2) {
type = params[1];
}
}
response.setCharacterEncoding(type);
}