gpt4 book ai didi

java - 有没有办法在 App Engine 中使用 UTF-8?

转载 作者:太空狗 更新时间:2023-10-29 22:52:45 27 4
gpt4 key购买 nike

我正在寻找有关应用引擎如何处理字符编码的解释。我正在开发客户端-服务器应用程序,其中服务器位于 App Engine 上。

这是一个从头开始构建的新应用程序,因此我们在所有地方都使用 UTF-8。客户端通过POST向服务器发送一些字符串,x-www-form-urlencoded。我收到它们并回应它们。当客户取回它时,它是 ISO-8859-1!当 POST 到 blobstore 时,我也看到了这种行为,参数以 UTF-8 格式发送,多部分/表单数据编码。

郑重声明,我在 Wireshark 中看到了这一点。所以我 100% 确定我发送 UTF-8 并接收 ISO-8859-1。另外,我没有看到 mojibake:ISO-8859-1 编码的字符串非常好。这也不是误解内容类型的问题。这不是客户端。一路上正确识别我正在发送 UTF-8 参数,但出于某种原因正在将它们转换为 ISO-8859-1。

我相信 ISO-8859-1 是 GAE servlet 的默认字符编码。我的问题是,有没有办法告诉 GAE 不要转换为 ISO-8859-1 而是到处使用 UTF-8?

假设 servlet 做这样的事情:

public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
resp.setContentType("application/json");
String name = req.getParameter("name");
String json = "{\"name\":\"" + name + "\"}";
resp.getOutputStream().print(json);
}

我尝试将响应和请求的字符编码设置为“UTF-8”,但这并没有改变任何东西。

提前致谢

最佳答案

我看到您应该做两件事。

1) 在您的 appengine-web.xml 中将系统属性(如果您正在使用它)设置为 utf8

<system-properties>
<property name="java.util.logging.config.file" value="WEB-INF/logging.properties" />
<property name="file.encoding" value="UTF-8" />
<property name="DEFAULT_ENCODING" value="UTF-8" />
</system-properties>

好的,以上就是我所拥有的,但文档建议如下:

<env-variables>
<env-var name="DEFAULT_ENCODING" value="UTF-8" />
</env-variables>

https://developers.google.com/appengine/docs/java/config/appconfig

2) 在设置内容类型时指定编码,否则将恢复为默认值

The content type may include the type of character encoding used, for example, text/html; charset=ISO-8859-4.

我会试试

resp.setContentType("application/json; charset=UTF-8");

您也可以尝试让您直接为其设置内容类型的编写器。

http://docs.oracle.com/javaee/1.3/api/javax/servlet/ServletResponse.html#getWriter%28%29
http://docs.oracle.com/javaee/1.3/api/javax/servlet/ServletResponse.html#setContentType(java.lang.String)

无论如何,我需要 utf8 来处理日文内容,而且我没有遇到任何问题。无论如何,我没有使用过滤器或 setContentType。我正在使用上面的 gwt 和 #1 并且它有效。

关于java - 有没有办法在 App Engine 中使用 UTF-8?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11907764/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com