把web工程的war包发布到Linux服务器上总是报错exception is com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8 sequence
,具体如下:
[2015-09-02 09:32:52 uq_www] INFO [pool-2-thread-3] XmlBeanDefinitionReader.loadBeanDefinitions(315) | Loading XML bean definitions from class path resource [applicationContext.xml]
[2015-09-02 09:32:53 uq_www] INFO [pool-2-thread-3] XmlBeanDefinitionReader.loadBeanDefinitions(315) | Loading XML bean definitions from class path resource [applicationContext/db-context.xml]
[2015-09-02 09:32:53 uq_www] ERROR [pool-2-thread-3] ContextLoader.initWebApplicationContext(331) | Context initialization failed
org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [applicationContext/db-context.xml]
Offending resource: class path resource [applicationContext.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from class path resource [applicationContext/db-context.xml]; nested exception is com.sun.org.apache.xerces.internal.impl.io.MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8 sequence
对比db-context.xml文件的编码方式是UTF-8,文件首行是
<?xml version="1.0" encoding="UTF-8"?>
也不错。然后使用maven build,命令是clean package。
生成的war包里,使用notepad++打开db-context.xml总是有部分中文注释乱码,但在eclipse里显示是没有乱码的。想必就是此问题了。于是暂时把问题定位
在maven的插件上,根据一些博客的提示陆续把pom.xml做成这样的配置:
plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<configuration>
<warSourceDirectory>WebRoot</warSourceDirectory>
<webResources>
<resource>
<directory>${package.environment}</directory>
<targetPath>WEB-INF/classes</targetPath>
<filtering>true</filtering>
</resource>
</webResources>
</configuration>
<version>2.2</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.4</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>2.6</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
还有这样:
同样不可以。
那么接下来莫非是我eclipse环境有问题,于是这样:
- 修改Eclipse中文本文件的默认编码:windows -> Preference -> general -> Workspace -> Text file encoding设置为UTF-8
- 修改JAVA源文件的默认编码:windows->Preferences->general->Content Types->右侧Context Types树,点开Text,选择Java Source File,在下面的Default encoding输入框中输入UTF-8,点Updat
这样就可以了。后来我又把pom.xml配置改为原来的,即不配置maven-resources-plugin的编码方式,也删除<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
此项,然而生成的war包仍是正常的。这样看来其实是eclipse的环境问题了。