使用的是spring-boot項目,天然而然的使用spring-boot-maven-plugin插件對項目進行打包操做,因爲項目中docx文檔做爲模板文檔,以前一直運行出錯,去看了下target下面的這個文檔(有的時候FileNotFoundException或者出現各類文件錯誤,去看target仍是比較真實的),而後打開一看,全亂碼了,如圖: css
正常的應該爲:實驗了各類網上的方案,我搜索到的基本都是說maven或者spring-boot-maven-plugin致使excel文檔出現問題的。
1. 修改編碼爲UTF-8
<plugin>
<!--打包成可執行jar-->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<jvmArguments>-Dfile.encoding=UTF-8</jvmArguments>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
2. 使用另一種打包方案
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>本身的主類(含有main方法的類)的全類名</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
3. 查看本身項目是否設置爲UTF-8,以及對應全部的文件是否設置爲UTF-8
看了,全是UTF-8
複製代碼
以上方法所有試驗愣是沒解決。html
最後拿着一個項目做比對,發現了一個小區別:
正常的項目(maven項目,不是spring-boot類型的項目,打包使用的是maven-compiler-plugin這個插件)的resource配置有個區別:
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.json</include>
<include>**/*.yml</include>
<include>**/*.js</include>
<include>**/*.html</include>
<include>**/*.csv</include>
<include>**/*.sqlite</include>
<include>**/*.docx</include>
<include>**/*.zip</include>
</includes>
</resource>
非正常項目的resource(這個是maven的spring-boot項目,使用:spring-boot-maven-plugin這個插件)爲:
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.json</include>
<include>**/*.yml</include>
<include>**/*.js</include>
<include>**/*.html</include>
<include>**/*.css</include>
<include>**/*.svg</include>
<include>**/*.jpg</include>
<include>**/*.jpeg</include>
<include>**/*.png</include>
<include>**/*.ico</include>
<include>**/*.woff2</include>
<include>**/*.txt</include>
<include>**/*.xlsx</include>
<include>**/*.xls</include>
<include>**/*.docx</include>
</includes>
<excludes>
<exclude>env/*.properties</exclude>
</excludes>
</resource>
他倆的區別就是,不正常的這個resource配置多了個:<filtering>true</filtering>這個選項
複製代碼
結果去除掉true以後,正常了。去除以後的resource爲:java
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.json</include>
<include>**/*.yml</include>
<include>**/*.js</include>
<include>**/*.html</include>
<include>**/*.css</include>
<include>**/*.svg</include>
<include>**/*.jpg</include>
<include>**/*.jpeg</include>
<include>**/*.png</include>
<include>**/*.ico</include>
<include>**/*.woff2</include>
<include>**/*.txt</include>
<include>**/*.xlsx</include>
<include>**/*.xls</include>
<include>**/*.docx</include>
</includes>
<excludes>
<exclude>env/*.properties</exclude>
</excludes>
</resource>
複製代碼
我也不知道什麼緣由。搜索了一下大體解釋以下:
resource的filtering配置:
主要用來替換項目中的資源文件(*.xml、*.properties)當中的${...},好比${db.url},
那麼若是配置了db.url=aaa的話,在項目編譯的時候,就會自動的把${db.url}替換爲aaa
也就是說,多是在打包的時候,docx裏面有好多${xxxxx}形式的東西,而後致使出現替換的問題,可是我查看了下文檔,
好像也不是那麼一回事,具體緣由不明白,上面這個緣由只是一個猜想。
複製代碼
若是去除掉了true 或者設置爲false ,那麼隨之而來的一個問題就是:你的maven打包多環境替換properteies將會失效。 想起來一句話:此事古難全,希望人長久!spring
今天實驗了下,有個解決方案,原來是如此的簡單,我居然沒有想到:解決方案就是,多寫幾個resource標籤就行,全部的二進制形式的文件,或者亂碼的文件都放置在不須要替換的resource標籤下面 例如:sql
<resources>
//這個是java的文件操做,不須要過濾
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
//這個是真正的資源文件,須要過濾的
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.json</include>
<include>**/*.yml</include>
<include>**/*.js</include>
<include>**/*.html</include>
<include>**/*.css</include>
<include>**/*.svg</include>
<include>**/*.jpg</include>
<include>**/*.jpeg</include>
<include>**/*.png</include>
<include>**/*.ico</include>
<include>**/*.woff2</include>
<include>**/*.txt</include>
<include>**/*.xlsx</include>
<include>**/*.xls</include>
</includes>
<excludes>
<exclude>env/*.properties</exclude>
</excludes>
</resource>
//這個也是資源文件,不須要過濾的,能夠單獨拉出來設置filtering爲false
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/*.docx</include>
</includes>
</resource>
</resources>
複製代碼