invalid stream header: EFBFBDEF 問題解決

u清發表於2018-12-29

我們專案使用report 報表功能,然後在載入xxxx.jasper檔案時候報的invalid stream header: EFBFBDEF 的錯誤

public JasperPrint fill(InputStream inputStream, Map<String, Object> parameters)throws JRException{

 JasperReport jasperReport = (JasperReport) JRLoader.loadObject(inputStream); //這裡出錯

 return fill(jasperReport, parameters);
 }

 

跟蹤原始碼主要是下面出錯,百思不得其解,網上說很多都是發現字元被改變了以至於ObjectOutputStream無法識別該字元陣列所以丟擲了java.io.StreamCorruptedException: invalid stream header: EFBFBDEF,可是我這個跟字串沒關係。inputStream讀取的是二進位制檔案,怎麼可能出現這個問題。

    ObjectInputStream in = new ObjectInputStream(inputStream);

偶然看到這篇文章才得以解決https://stackoverflow.com/questions/24078820/java-io-streamcorruptedexception-invalid-stream-header-efbfbdef

 

主要是跟maven resource 標籤有關係,maven 打包時候,已經把二進位制檔案給破壞了,導致失敗

 

<resources>
<
resource>
<targetPath>${project.build.directory}/classes</targetPath> <directory>src/main/resources</directory> <filtering>true</filtering> </resource>
</resources>

在maven 網站過濾的時候

警告:不要過濾包含影像等二進位制內容的檔案!這很可能會導致輸出損壞。如果您同時擁有文字檔案和二進位制檔案作為資源,則需要宣告兩個互斥的資源集。第一個資源集定義要過濾的檔案,另一個資源集定義要保持不變的檔案。

以下則沒有問題


<
resources> <resource> <targetPath>${project.build.directory}/classes</targetPath> <directory>src/main/resources</directory> <filtering>true</filtering> <excludes> <exclude>**/*.jasper</exclude> <exclude>**/*.jrxml</exclude> </excludes> </resource> <resource> <targetPath>${project.build.directory}/classes</targetPath> <directory>src/main/resources</directory> <filtering>false</filtering> <includes> <include>**/*.jasper</include> <include>**/*.jrxml</include> </includes> </resource>
</
resources>

 

相關文章