如何使用 Spring/Roo 对 POST 的特殊字符进行编码

发布于 2024-08-27 23:04:21 字数 115 浏览 5 评论 0原文

我正在使用 Spring/Roo 作为应用程序服务器,并且需要能够发布一些特殊字符。具体来说,是日元符号或欧元符号等字符。当我在服务器上收到这些字符并将它们显示在控制台中时,它们显示为“?”。如何正确编码和接收它们?

I'm using Spring/Roo for an app server, and need to be able to post some special characters. Specifically, characters like the Yen symbol, or Euro symbol. When I receive these characters on my server, and display them in console, they appear as "?". How can they be properly encoded and received?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

赠意 2024-09-03 23:04:21

尝试将 src/main/resources/META-INF/spring/database.properties 配置为:

database.url=jdbc:mysql://[YOUR_DB_SERVER]:3306/[YOUR_DB_NAME]?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8

Try configuring src/main/resources/META-INF/spring/database.properties to this :

database.url=jdbc:mysql://[YOUR_DB_SERVER]:3306/[YOUR_DB_NAME]?autoReconnect=true&useUnicode=true&characterEncoding=UTF-8
[旋木] 2024-09-03 23:04:21

这里有几个可能的故障点。

首先,我会检查控制台是否支持有问题的字符:

  • 如果 JVM 使用的默认编码不支持这些字符,则 System.out 会将它们转换为问号,
  • 如果控制台字体不支持这些字符,
  • 如果控制台使用与 System.out 编码不同的编码来解码字节,则这些字符将无法正确显示

不要尝试将字符打印为文字,而是转换为 int 并打印十六进制值 - 然后根据 Unicode 图表

浏览器和服务器之间也可能发生有损或不正确的转换。理想情况下,服务器应该使用 UTF-8 进行编码和解码。如果浏览器在对数据进行编码时使用的编码不支持这些字符,则会对它们进行有损编码;浏览器通常根据服务器为 GET 请求发送的编码来选择编码(或者更罕见地从 表单属性)。检查与您的数据一起发送的 Accept-Charset 标头(您可以使用 FirebugFiddler)。我对 Roo 一无所知,但肯定有某种配置编码的机制。

There are a couple of possible failure points here.

First, I'd check to see if the console supports the characters in question:

  • if the default encoding used by the JVM does not support the characters, they will be turned into question marks by System.out
  • if the console font does not support the characters, they will not be rendered properly
  • if the console is decoding the bytes using a different encoding to the one System.out is encoding them to, the characters will not display correctly

Instead of trying to print characters as literal, cast to int and print the hex value - then check the value against the Unicode charts.

Lossy or incorrect conversions can also happen between the browser and the server. Ideally, the server should use UTF-8 for encoding and decoding. If the encoding used by the browser when it encodes the data does not support the characters, they will be lossily encoded; the browser usually picks an encoding based on the encoding sent by the server for the GET request (or more rarely from a form attribute). Inspect the Accept-Charset header being sent with your data (you can do this with something like Firebug or Fiddler). I don't know anything about Roo, but there's bound to be some mechanism to configure encodings.

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