标题翻译
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 - 请在可用时运行并提供初始更新;感谢您的接受! ��</***QuestionHere***>
我想要将数据� � 转换为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'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("UTF-8"));
// 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! ��</QuestionHere>
I want to convert the data &#55357 ;&#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 < 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>
专注分享java语言的经验与见解,让所有开发者获益!
评论