问题描述
getResourceAsStream()刚遇到一个非常奇怪的问题.
I have just run into a very strange problem with getResourceAsStream().
在产品项目JUnit测试中,我使用getResourceAsStream()读取测试数据,我发现getResourceAsStream()有时会替换某些字节:
In my prod project JUnit test I read test data using getResourceAsStream(), I found that getResourceAsStream() sometimes substitutes some bytes:
byte[] fileBytes = FileUtils.readFileToByteArray(new File(
"resources/test/parser/test-short-enc.xml"));
printBytes(fileBytes);
byte[] classPathBytes = IOUtils.toByteArray(ParserTest.class
.getResourceAsStream("/test/parser/test-short-enc.xml"))
printBytes(classPathBytes);
在此项目中,输出如下所示:
In this project output looks like this:
D0 9A D1 80 D0 B8 D0 BC D0 B8 D0 BD D0 B0 D0 BB D0 B8 D1 81 D1 82 D0 B8 D0 BA D0 B0
D0 9A D1 80 D0 B8 D0 BC D0 B8 D0 BD D0 B0 D0 BB D0 B8 D1 81 D1 82 D0 B8 D0 BA D0 B0
D0 9A D1 80 D0 B8 D0 BC D0 B8 D0 BD D0 B0 D0 BB D0 B8 D1 3F D1 82 D0 B8 D0 BA D0 B0
D0 9A D1 80 D0 B8 D0 BC D0 B8 D0 BD D0 B0 D0 BB D0 B8 D1 3F D1 82 D0 B8 D0 BA D0 B0
此后,我决定创建一个显示bug的小项目,并将其托管在Github上作为示例.这是链接: https://github.com/snowindy/getResourceAsStream-Bug
After this, I decided to create a small bug-showing project and host it at Github as an example. Here's the link: https://github.com/snowindy/getResourceAsStream-Bug
我基本上复制了所需的代码,运行后,我看不到问题重现:
I essentially copied the code needed, after run, I could not see the problem reproducing:
D0 9A D1 80 D0 B8 D0 BC D0 B8 D0 BD D0 B0 D0 BB D0 B8 D1 81 D1 82 D0 B8 D0 BA D0 B0
D0 9A D1 80 D0 B8 D0 BC D0 B8 D0 BD D0 B0 D0 BB D0 B8 D1 81 D1 82 D0 B8 D0 BA D0 B0
D0 9A D1 80 D0 B8 D0 BC D0 B8 D0 BD D0 B0 D0 BB D0 B8 D1 81 D1 82 D0 B8 D0 BA D0 B0
D0 9A D1 80 D0 B8 D0 BC D0 B8 D0 BD D0 B0 D0 BB D0 B8 D1 81 D1 82 D0 B8 D0 BA D0 B0
printBytes函数如下所示:
The printBytes function looks like this:
public static void printBytes(byte[] bv) {
System.out.println();
for (byte b : bv) {
System.out.print(' ');
System.out.print(String.format("%02X", b));
}
}
那会是什么?
我使用eclipse,UTF-8工作区编码,该文件包含cyrilic单词Криминалистика",这是UTF-8 no-BOM文件.
I use eclipse, UTF-8 workspace encoding, the file contains cyrilic word "Криминалистика", it's a UTF-8 no-BOM file.
我在两个项目中都使用JavaSE-1.6(jdk1.6.0_29),我有Windows 7操作系统,Windows-1252系统编码.
I use JavaSE-1.6 (jdk1.6.0_29) for both projects,I have Windows 7 OS, windows-1252 system encoding.
更新
我终于能够重现该错误.我更新了该项目,以便您可以对其进行测试: https://github.com/snowindy/getResourceAsStream-Bug
I was finally able to reproduce the bug. I updated the project so you can test it: https://github.com/snowindy/getResourceAsStream-Bug
仅当您在maven pom.xml中包含此代码时,该错误才会出现.这意味着它是特定于Maven的
The bug appears only if you have this code in maven pom.xml. This means it's maven-specific
<build>
<sourceDirectory>src</sourceDirectory>
<resources>
<resource>
<directory>resources</directory>
<filtering>true</filtering>
</resource>
</resources>
...
推荐答案
好,我知道了.
此配置可解决问题:
<project>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
...
我从这个答案中得到启发: https://stackoverflow.com/a/8979120/792313
I got inspired with this answer: https://stackoverflow.com/a/8979120/792313
这篇关于Java class.getResourceAsStream()返回不正确的字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!