更改HttpServletResponse的编码 [英] Change encoding of HttpServletResponse

查看:134
本文介绍了更改HttpServletResponse的编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回XML的API,它实际上使用默认的编码返回它(我相信它是UTF-8),但是现在的需求已经改变了,我们需要返回所有的UTF-16LE。



我的问题是:有一个简单的方法吗?我可以在电话完成之前访问响应,所以我想知道我是否可以做一些像

  //这个方法不存在
response.setCharacterEncoding(UTF-16LE);

非常感谢!



更新:
提到的方法是使用的方法。我使用的是没有包含它的servlet API的旧版本(2.3)。

解决方案

呃,方法存在, here


设置发送给客户端
的响应的字符编码(MIME
charset),for例如UTF-8。如果
,字符编码已经由
setContentType(java.lang.String)或
setLocale(java.util.Locale)设置,
方法覆盖它。调用
setContentType(java.lang.String)与
文本/ html的字符串并调用
此方法与UTF-8的字符串
相当于调用
setContentType,字符串为
text / html; charset = UTF-8。



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.

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.

解决方案

Uhh, the method does exist, 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.

这篇关于更改HttpServletResponse的编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆