更改 HttpServletResponse 的编码
我有一个返回 XML 的 API,它实际上使用默认编码(我相信它是 UTF-8)返回它,但现在要求已经改变,我们需要以 UTF-16LE 返回所有内容。
我的问题是:有没有一种简单的方法可以做到这一点?我可以在通话完成之前获得回复,所以我想知道我是否可以做类似
//This method does not exist
response.setCharacterEncoding("UTF-16LE");
“非常感谢!”之类的事情!
更新: 所提到的方法就是要使用的方法。我使用的是旧版本 (2.3) 的 servlet API,但不包含它。更改版本解决了这一切。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
呃,该方法确实存在,此处
Uhh, the method does exist, here
正如其他人所说,使用:
或:
...但请确保在调用 response.getWriter(); 之前执行此操作; ...!
As others have stated, use either:
or:
...but make sure you do this before calling response.getWriter(); ...!
首先
然后,确保您确实发出该编码!
First
Then, make sure you're actually emitting that encoding!
我发现您必须将字符编码至少设置为 UTF-8,因为默认值是 ISO-8859-1。 ISO-8859-1 字符集不考虑某些扩展字符。我编写了一个辅助函数来使用“Accept”标头中发送的内容:
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:
只需执行以下操作:
just do the following thing: