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

我们有一个场景,页面提交多个同名字段。为了解决 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");

【讨论】:

  • 我昨天真的想出了这个解决方案。为什么会这样,我不确定。不过会检查网络服务器的编码。
  • 几年前我在 cmets 中针对我的代码编写了这个 - 它在 Win 2008 R2 / IIS7.5 盒子上:灵感来自使用 *.com/questions/39924847/… 作为 *.com/questions/2764446/… -alternative 的解决方案MS 修补程序:support.microsoft.com/en-us/topic/…
  • 我不确定 utf-8 服务器变量选项在什么时候成为 IIS 中的标准功能。我不记得以后必须修补服务器。