本文介绍了非法字符 - CTRL-CHAR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到来自网络服务的以下异常:

I am getting following exception from webservices:

com.ctc.wstx.exc.WstxUnexpectedCharException: 非法字符 ((CTRL-CHAR, code 15))

我知道这背后的原因,我得到了控制字符";在我想返回的数据中.并且在 XML 中 CTRL-CHAR 是不允许的.

I know the reason behind this, I am getting "Control Characters" in data I want to return. And in XML CTRL-CHAR are not allowed.

我搜索了解决方案,在很多地方我找到了删除CTRL-CHAR的代码.

I searched for the solution, and many places I found the code to remove CTRL-CHAR.

担心的是,如果我从数据中删除控制字符,我最终会丢失数据吗?
我想要干净的解决方案可能编码,而不是删除控制字符.

The concern is shall I end up loss of data if I remove control characters from data?
I want the clean solution may encoding, instead of removing control char.

推荐答案

感谢大家的投入.我正在分享解决方案可能对其他人有帮助.要求不是消除 CONTROL CHAR,它也应该保持在 DB 中的原样,并且一个 WS 通过 n/w 客户端发送它应该能够获得 CONTROL CHAR.所以我实现了如下代码:

Thanks guys for you inputs. I am sharing solution might be helpful for others.The requirement was not to wipe out CONTROL CHAR, it should remain as it is in DB also and one WS sends it across n/w client should able to get the CONTROL CHAR. So I implemented the code as follow:

  1. 在 Web 服务代码中使用 URLEncoder 对字符串进行编码.
  2. 在客户端使用 URLDecoder 对其进行解码

在下面分享示例代码和输出.
示例代码:

Sharing sample code and output bellow.
Sample code:

System.out.println("NewSfn");
System.out.println(URLEncoder.encode("NewSfn", "UTF-8"));
System.out.println(URLDecoder.decode("NewSfn", "UTF-8"));

输出:

NewSfn
New%0FSfn
NewSfn

因此客户端将收到控制字符.

So client will recieve CONTROL CHARs.

堆栈交换没有显示上面的控制字符.NewSfn 就是这样的New(CONTROL CHAR)Sfn.

Stack Exchange is not showing CONTROL CHAR above. NewSfn is like this New(CONTROL CHAR)Sfn.

这篇关于非法字符 - CTRL-CHAR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-04 06:35
查看更多