如何在Servlet响应中转换代理对?如何读取代理对?

huangapple 未分类评论42阅读模式
标题翻译

How to Convert Surrogate Pairs in Servlet Response? How can I read Surrogate Pairs?

问题

我在响应中收到了代理对作为代理对的表情符号。

我想要将这些代理对更改为Unicode,以便WebSphere Portal 7能够理解这些Unicode。

我添加了一个过滤器来修改响应,将代理对转换为Unicode,但是在这种情况下我无法进行转换。



**过滤器:**
```java
    public void doFilter(
            ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException
    {
        CharResponseWrapper wrappedResponse = new CharResponseWrapper(
                (HttpServletResponse)response);

        chain.doFilter(request, wrappedResponse);
       byte[] bytes = wrappedResponse.getByteArray();
         String out = new String(bytes);      
         String inputXml = new String(out.getBytes("UTF-8")); 
         // 需要在这里将代理对从XML字符串转换为Unicode,并将其写回响应
         out = surrogatesToUnicode(inputXml);      
         response.getOutputStream().write(out.getBytes());
    }

响应:

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
	<SOAP-ENV:Header/>
	<SOAP-ENV:Body>
		<xxxxx>
			<xx>
				<xx>
					<xx></xx>
					<xx>100420</xx>
					<xx>xxx</xx>
					<xx>4</xx>
					<xx>false</xx>
					<xx>false</xx>
					<xx>false</xx>
					<***QuestionHere***>Agent - 请在可用时运行并提供初始更新;感谢您的接受! &#55357;&#56842;</***QuestionHere***>

我想要将数据&#55357; &#56842; 转换为Unicode。我正在使用此函数,但是它无法在charCount方法中识别代理对,并从响应字符串进行转换。

 public String surrogatesToUnicode(String str) {
        StringBuilder sb = new StringBuilder();           
       
        for(int i = 0; i < str.length(); i ++) {
            int cp = str.codePointAt(i);   
            if(Character.charCount(cp) == 2) {
            	sb.append("&#" + cp + ";");
            	i++;
            }        
           else {
            sb.append(Character.toChars(cp));
            }
        }
        return sb.toString();
    }

<details>
<summary>英文翻译</summary>

I have emoji as Surrogate Pairs received in the response. 

I want to change these Surrogates Pairs to a Unicode so that the WebSphere Portal 7 will understand the Unicodes. 

Added a filter to modify the response, to convert Surrogates to Unicode but I&#39;m unable to convert in this situation.



**Filter:** 
public void doFilter(
        ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException
{
    CharResponseWrapper wrappedResponse = new CharResponseWrapper(
            (HttpServletResponse)response);

    chain.doFilter(request, wrappedResponse);
   byte[] bytes = wrappedResponse.getByteArray();
     String out = new String(bytes);      
     String inputXml = new String(out.getBytes(&quot;UTF-8&quot;)); 
     // Need to Convert the Surrogates to Unicode from the XML String here and write it back to Response
     out = surrogatesToUnicode(inputXml);      
     response.getOutputStream().write(out.getBytes());
}

**Response:**

<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<xxxxx>
<xx>
<xx>
<xx></xx>
<xx>100420</xx>
<xx>xxx</xx>
<xx>4</xx>
<xx>false</xx>
<xx>false</xx>
<xx>false</xx>
<QuestionHere>Agent - Please run and advise with initial update when available; thank you for accepting! &#55357;&#56842;</QuestionHere>



I want to convert the data &amp;#55357 ;&amp;#56842 ; into Unicode. And I am using this function but it is unable to recognize the surrogate pairs at the charCount method and convert from the response String. 

public String surrogatesToUnicode(String str) {
StringBuilder sb = new StringBuilder();

    for(int i = 0; i &lt; str.length(); i ++) {
        int cp = str.codePointAt(i);   
        if(Character.charCount(cp) == 2) {
        	sb.append(&quot;&amp;#&quot; + cp + &quot;;&quot;);
        	i++;
        }        
       else {
        sb.append(Character.toChars(cp));
        }
    }
    return sb.toString();
}




</details>


huangapple
  • 本文由 发表于 2020年3月16日 22:40:07
  • 转载请务必保留本文链接:https://java.coder-hub.com/60707974.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定