2011-06-21 19 views
0

我通過HTTP接收JSON請願書。當從Internet Explorer 8解析未來失敗例外:JSON解析JAVA問題與控制字符

InPart inPart = mp.next(); 

MyClass myClass = inPart.getBody(MyClass.class, null); 

com.ctc.wstx.exc.WstxUnexpectedCharException: Illegal character ((CTRL-CHAR, code 12)) at [row,col {unknown-source}]: [1,101]] 

相關的代碼:

InPart inPart = mp.next(); 

String s = inPart.getBody(String.class, null); 

providers.getMessageBodyReader(MyClass.class, null, null, 
      MediaType.APPLICATION_JSON_TYPE).readFrom(MyClass.class, null, null, 
      MediaType.APPLICATION_JSON_TYPE, headers.getRequestHeaders(), 
       new ByteArrayInputStream(s.getBytes())); // Tried with s.getBytes("UTF-8") 

com.ctc.wstx.exc.WstxUnexpectedCharException: Illegal character ((CTRL-CHAR, code 12)) at [row,col {unknown-source}]: [1,101] 

而且,如果我做的:

String ss = s.replaceAll("\\p{Cntrl}", ""); 

ss.equals(s); // 

輸出真正的

長度是相同。

我也試過:

private String removeControlChar(String in) { 
    StringBuilder sb = new StringBuilder(); 
    for (char c : in.toCharArray()) 
    { 
     if(!Character.isISOControl(c)) { 
      sb.append(c); 
     } 
     else 
     { 
      // To delete 
      int i = 0; 
     } 
    } 
    return sb.toString(); 
} 

InPart inPart = mp.next(); 

String s = inPart.getBody(String.class, null); 
Strign ss = removeControlChar(s); 
providers.getMessageBodyReader(MyClass.class, null, null, 
      MediaType.APPLICATION_JSON_TYPE).readFrom(MyClass.class, null, null, 
      MediaType.APPLICATION_JSON_TYPE, headers.getRequestHeaders(), 
       new ByteArrayInputStream(ss.getBytes())); // Tried with s.getBytes("UTF-8") 

如果我調試,從而未能字符是\f作爲例外規定。但是錯誤表明它是一個無效的XML字符。這可能是問題嗎?

任何想法?這似乎隻影響Internet Explorer。

謝謝。

回答

0

我不會刪除這個問題,以防萬一任何人陷入同樣的​​困境。

問題是瀏覽器發送像C:\fakepath\這樣的路徑,在執行時,在轉換爲XML之後,它指出\f

您可以在瀏覽器中設置此行爲。

問候。