最近项目中有生成word的功能,需要读取模板生成对应word文件,模板放在资源文件夹resource下,但是模板文件明明存在,就是读取不到程序报错
com.aspose.words.FileCorruptedException: The document appears to be corrupted and cannot be loaded.
at com.aspose.words.FileFormatUtil.zzZ(Unknown Source)
at com.aspose.words.Document.zzY(Unknown Source)
at com.aspose.words.Document.zzZ(Unknown Source)
at com.aspose.words.Document.<init>(Unknown Source)
at com.aspose.words.Document.<init>(Unknown Source)
at com.aspose.words.Document.<init>(Unknown Source)
将jar包解压之后查看对应模板文件,发现不能正常打开,果然是打包后资源文件出现问题。原来,打包时,默认情况下会将资源文件统一进行编译,这样会导致资源文件不能访问或访问之后乱码,解决办法是打包时使用maven插件排除调指定文件即可
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>sql</nonFilteredFileExtension>
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
<nonFilteredFileExtension>xls</nonFilteredFileExtension>
<nonFilteredFileExtension>doc</nonFilteredFileExtension>
<nonFilteredFileExtension>docx</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
加入此配置之后,项目正常运行,jar包解压之后,资源文件可以正常打开,完美解决。