2012-12-06 21 views
1

我正在使用PrimeFaces FileUpload組件將.properties文件傳輸到服務器。然而,擴展並不是一切,所以我想在發佈其他內容時測試行爲。我上傳樣本jar文件(apache的公地編解碼器是特定的),但不是在堆棧跟蹤可能是個例外,我整個瀏覽器的怪異行爲就來了:對話內容完全地崩潰了,不方便(IE) 。使用二進制文件PrimeFaces FileUpload錯誤

我打開了JavaScript控制檯,我發現了更基本的錯誤。

火狐,還有的jQuery的錯誤,但是對話並沒有崩潰:

NS_ERROR_NOT_IMPLEMENTED: Component returned failure code: 0x80004001 (NS_ERROR_NOT_IMPLEMENTED) [nsIDOMLSProgressEvent.input] 

IE 9,然而,有一個從渲染引擎中的錯誤:

XML5617: Ungültiges XML-Zeichen. 
form.xhtml, Zeile 3 Zeichen 3926 

XML答案包含二進制內容,例如上傳文件的內容將附加到其中。尋找可能的PrimeFaces錯誤我發現了以下內容:primefaces fileupload filter with utf8 characters filter但我不知道它如何適用於我的情況,因爲我沒有將內容存儲到字符串中,我直接操作UploadedFile對象:

public void onPropertyFileUpload(FileUploadEvent event) { 
    log.info("onPropertyFileUpload"); 
    if (event.getFile() == null) { 
     log.warn("Empty file!!!"); 
     return; 
    } 
    Properties props = new Properties(); 
    try { 
     props.load(event.getFile().getInputstream()); 
    } catch (IOException e) { 
     log.error(e.getMessage(), e); 
     return; 
    } 

那麼,是BalusC發現這個問題的原因在我的情況在MultipartRequest的bug,或者這是什麼東西?而且,最重要的是,我能做些什麼來避免這個錯誤?

回答

1

起初我看過primefaces fileupload filter with utf8 characters filter,我認爲這可能是這種情況,但在對PrimeFaces代碼進行分析後,我發現這是完全別的東西。

的錯誤其實是在PrimeFaces,卻深深隱藏。錯誤在於他們沒有正確地轉義它們嵌入到XML響應中的文本。我的代碼將文件轉換爲字符串,該字符串可用於編輯,因此發送給用戶。由於上傳的文件是二進制文件,並且PrimeFaces使無法正確轉義,所以XML被破壞。

因爲實際上有沒有辦法在Java中說,該字符串是正確的(無解碼錯誤!),我不得不使用代碼我已經在其他項目中見過幾次面(應該不是至少被放入阿帕奇公地琅

/** 
    * This method ensures that the output String has only 
    * valid XML unicode characters as specified by the 
    * XML 1.0 standard. For reference, please see 
    * <a href="http://www.w3.org/TR/2000/REC-xml-20001006#NT-Char">the 
    * standard</a>. This method will return an empty 
    * String if the input is null or empty. 
    * 
    * @param in The String whose non-valid characters we want to remove. 
    * @return The in String, stripped of non-valid characters. 
    */ 
    public String stripNonValidXMLCharacters(String in) { 
     StringBuffer out = new StringBuffer(); // Used to hold the output. 
     char current; // Used to reference the current character. 

     if (in == null || ("".equals(in))) return ""; // vacancy test. 
     for (int i = 0; i < in.length(); i++) { 
      current = in.charAt(i); // NOTE: No IndexOutOfBoundsException caught here; it should not happen. 
      if ((current == 0x9) || 
       (current == 0xA) || 
       (current == 0xD) || 
       ((current >= 0x20) && (current <= 0xD7FF)) || 
       ((current >= 0xE000) && (current <= 0xFFFD)) || 
       ((current >= 0x10000) && (current <= 0x10FFFF))) 
       out.append(current); 
     } 
     return out.toString(); 
    } 

溶液的來源:https://kr.forums.oracle.com/forums/thread.jspa?threadID=1625928

相關問題