首页 > 解决方案 > 如何在 Java 应用程序中允许特殊字符,如破折号?

问题描述

如何在 Java 应用程序中允许特殊字符,如破折号?

这个问题是当用户复制并粘贴 Word 文档中的值然后系统显示“?” 而不是破折号“-”字符。复制字符串有破折号字符而不是连字符。Em dash 和 hyphen (En dash) 是不同的字符,Em dash 字符不是 ASCII 字符的一部分。它的字符代码是\u2014。我的应用程序不支持破折号字符。

示例 1:- 不工作:输入字符串:“Anil – Satija”和显示值:“Anil ? Satija” 示例 2:名称 =“Anil - Satija” - 连字符工作正常并显示正确的值。

客户端技术是 Angular 1.5.5,服务器端技术是 Spring5.2.2。将请求作为 POST 发送。该应用程序支持字符集“UTF-8”,内容类型为“application/xml”。我尝试在 API 中添加以下代码以接受所有 UTF-8 字符集。 index.html 元数据:

<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />

API代码是:

@RequestMapping(value = "/data", method = RequestMethod.POST)
  @Consumes("application/xml; charset=utf-8")
  public @ResponseBody
                String getData(HttpServletRequest request, HttpServletResponse response) throws Throwable {
                    RequestPayLoad requestPayLoad = parseRequest(request);    
         // requestPayLoad has corrupted value.
        // code
 }

在调试中,在客户端层 (xFactory.js) $scope.data 具有正确的值和破折号,但 API 层 (HttpRequest) 请求具有损坏的字符。因此,系统显示“?” 在这种情况下为损坏的字符。

标签: javaspring

解决方案


推荐阅读