Java 为什么输出文件比原始文件大?
我从资源文件夹中读取了一个文件,并试图写入另一个文件Java 为什么输出文件比原始文件大?,java,spring-boot,Java,Spring Boot,我从资源文件夹中读取了一个文件,并试图写入另一个文件 InputStream is = new ClassPathResource(fileName).getInputStream(); OutputStream outputStream = new FileOutputStream(fileName+"test"); byte[] buffer = new byte[8192]; int length; while ((length = is.read(buffe
InputStream is = new ClassPathResource(fileName).getInputStream();
OutputStream outputStream = new FileOutputStream(fileName+"test");
byte[] buffer = new byte[8192];
int length;
while ((length = is.read(buffer)) > 0) {
outputStream.write(buffer, 0, length);
}
outputStream.close();
is.close();
但输出文件未打开。我注意到:
原始~500kb的大小。
输出文件的大小为1Mb 我不明白文件大小问题的根本原因 我测试了pdf文件。
更新。
我编写了测试,这段代码可以工作,但在SpringBoot应用程序中不工作
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*.yml</include>
<include>**/*.xml</include>
<include>**/*.txt</include>
<include>**/*.json</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<includes>
<include>**/*.pdf</include>
</includes>
</resource>
</resources>
src/main/resources
真的
**/*yml先生
**/*.xml
**/*.txt
**/*.json
src/main/resources
假的
**/*.pdf
输出文件与输入文件是否相同?在代码中,您使用的是相同的文件名。@benw不,它们是不同的这里定义了ClassPathResource吗?也许它正在为您解压流,所以您正在编写解压后的图像。@DuncG ClassPathResource from org.springframework.core.io。如何压缩流?也许有另一种方法可以从资源文件夹中读取pdf文件?您可能需要为spring framework标记问题。通常,我会使用Class.getResourceAsStream(java.lang.String)
阅读类路径资源,但不使用Spring,因此无法帮助您。