在 Java 中处理 utf-8 字符串
我有一个应用程序,它使用 ajax 调用从域的活动目录中提取名称。有些名称包含西班牙语字符(例如 n 波形符)。我使用utf-8字符集来获取字符以正确显示表单上的数据。我可以成功地从 ajax 调用中提取名称并将其加载到表单字段中。我遇到的问题是,当将表单发布到服务器以进行数据库更新时,字符串转换会破坏扩展字符。
有没有特殊的 String 函数来处理 utf-8?将正确的值发布到 Oracle 表的正确方法是什么?
我已经完成了相当多的 Java 编码,但这是我第一次遇到扩展字符。任何帮助将不胜感激。
谢谢。
I have an application that pulls names from the active directory for the domain using ajax calls. Some names have Spanish characters (n tilde for example). I used the utf-8 character set to get the characters to correctly show the data on the form. I can successfully pull the names from the ajax call and load them into the form field. The problem I have is that when the form is posted to the server for the database update, the String cast corrupts the extended characters.
Is there a special String function to handle utf-8? What is the proper method to get the correct values posted to the Oracle tables?
I have done quite a bit of Java coding, but this is my first encounter with the extended characters. Any help will be appreciated.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个“演员”在哪里发挥作用?
我不确定你的应用程序是什么,但有几个地方你可能会破坏字符。首先,假设这是某种 Java EE 应用程序,请确保您已在 servlet 中设置请求编码。请参阅 HttpServletRequest 的 setCharacterEncoding 方法。您应该在那里使用“UTF-8”。
其次,您应该确保在表单变量上设置了accept-charset =“UTF-8”属性。 (注意 - 根据我的经验,如果页面一开始就是 UTF-8 编码的,这很少会成为问题,但安全总比后悔好)。
最后,确保您已指定连接数据库所需的任何编码选项。我不使用 Oracle,所以我不知道,但通常您需要在创建连接的地方指定使用“unicode”或“utf-8”或类似内容。
我会按顺序尝试它们,因为第一个本身可能可以解决问题。
Where is this "cast" coming into play?
I am not sure what your application is but there a couple of places where you could be mangling the characters. First, assuming this is some sort of Java EE app make sure that you have set the request encoding in the servlet. See the setCharacterEncoding method of HttpServletRequest. You should use "UTF-8" there.
Second, you should make sure that you have the accept-charset="UTF-8" attribute set on the form variable. (Note - in my experience this rarely is a problem if the page is UTF-8 encoded to begin with but better safe than sorry).
Last make sure that you have specified any encoding options if neccessary for the connection to the database. I don't use Oracle so I don't know but often you'll need to specify to use "unicode" or "utf-8" or the like somewhere where you create the connection.
I would try them in order because it's possible (likely) the first itself might fix the problem.
您需要一个 OutputStreamWriter。当您构造它时,请指定您要使用“UTF-8”字符集。还要确保您指定在 http 标头中发送 UTF-8。
You want an OutputStreamWriter. When you construct it, specify that you want to use the "UTF-8" charset. Also make sure you specify that you're sending UTF-8 in your http headers.