更改 HttpServletResponse 的编码

发布于 2024-08-13 17:19:24 字数 357 浏览 2 评论 0原文

我有一个返回 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

俯瞰星空 2024-08-20 17:19:24

呃,该方法确实存在,此处

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

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.

彼岸花ソ最美的依靠 2024-08-20 17:19:24

正如其他人所说,使用:

response.setCharacterEncoding("UTF-16LE");

或:

response.setHeader("Content-Type", "text/xml; charset=UTF-16LE");

...但请确保在调用 response.getWriter(); 之前执行此操作; ...!

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 before calling response.getWriter(); ...!

纸伞微斜 2024-08-20 17:19:24

首先

response.setHeader("Content-Type", "text/xml; charset=UTF-16LE");

然后,确保您确实发出该编码!

First

response.setHeader("Content-Type", "text/xml; charset=UTF-16LE");

Then, make sure you're actually emitting that encoding!

此岸叶落 2024-08-20 17:19:24

我发现您必须将字符编码至少设置为 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);
}

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:

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);
}
迷爱 2024-08-20 17:19:24

只需执行以下操作:

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();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文