【问题标题】:How to prevent unicode character corruption when using getPageContext().getRequest().getParameterValues()?使用 getPageContext().getRequest().getParameterValues() 时如何防止 unicode 字符损坏?
【发布时间】:2021-06-01 09:56:24
【问题描述】:

我们有一个场景,页面提交多个同名字段。为了解决 CF 的默认方法,将它们放入逗号分隔的字符串中,而不更改应用程序范围,我们使用 getPageContext().getRequest().getParameterValues("#fieldname#") 将某些位置的字段值作为数组访问。

我们遇到的问题是提交的 Unicode 字符已损坏。例如,字段数组中的El celular que compré está averiado 作为字符串El celular que compré está averiado 返回。如果我转储getHTTPRequestData(),我可以看到正确编码的El+celular+que+compr%C3%A9+est%C3%A1+averiado url 被发送到服务器。

CF 是否没有正确处理 java 字符串?无论如何要在非应用程序范围的基础上解决这个问题,而不是解析我们真的不想做的getHTTPRequestData().content

【问题讨论】:

  • ColdFusion 的哪个版本?

标签: coldfusion urlencode


【解决方案1】:

原因将是因为您的网络服务器没有在内部使用 utf-8 对其参数进行编码。在url 范围内访问变量时,您通常不会看到这一点,因为 CF 已经为您转换了它们,但是在查看 cgi.query_stringgetPageContext().getRequest().getParameterValues(...) 时您可以看到这种差异

在您的情况下,您似乎看到了windows-1252 编码。我在 IIS7.5 - IIS8 周围遇到了类似的问题。假设您不能或不想冒险尝试更改您的网络服务器配置,这种解决方法应该适合您:

webserverEncodedString = getPageContext().getRequest().getParameterValues(fieldname);
binaryValue = CharsetDecode(webserverEncodedString, "windows-1252");
utf8EncodedString = CharsetEncode(binaryValue, "utf-8");

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-25
  • 1970-01-01
  • 1970-01-01
  • 2018-08-26
  • 2014-11-26
相关资源
最近更新 更多