使用 Java Files.copy 复制后出现空白页的 PDF 文件

PDF file with empty pages appearing after Copying using Java Files.copy(使用 Java Files.copy 复制后出现空白页的 PDF 文件)

本文介绍了使用 Java Files.copy 复制后出现空白页的 PDF 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的类路径中的文件复制到另一个临时位置.

I am trying to copy a file in my Class path to another temp location.

这是它的代码:

    InputStream inputStream = this.getClass().getClassLoader()
            .getResourceAsStream(readmeFile);

    Path path = Paths.get(tempFilesOutputPath + File.separator + readmeFile);
    try {
        Files.copy(inputStream, path, StandardCopyOption.REPLACE_EXISTING);
        inputStream.close();

    } catch (IOException e) {
        throw e;
    }

readMeFile 有 2 页,tempFilesOutputPath 文件夹中的复制文件也有 2 页,但没有任何内容.

readMeFile has 2 pages, the copied file in the tempFilesOutputPath folder also has two pages but without any content.

如果我犯了一些错误或者必须以不同的方式完成,请告诉我.

Please let me know if I am making some mistake or it has to be done in a different way.

干杯,马杜

推荐答案

问题完全不相关.我正在使用maven复制资源来复制我的src/main/resources/下的资源

Issue was totally unrelated. I was using maven copy resource to copy the resources under my src/main/resources/

这是我的 Maven 资源:

this was my maven resource:

        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
                <include>**/*.txt</include>
                <include>**/*.html</include>
                <include>**/*.pdf</include>
            </includes>
        </resource>

由于过滤是针对 PDF 文件,因此被作为空文档复制到目标文件夹.

Since the filtering was on PDF file was copied as an empty doco to the target folder.

我只是将它分成两个资源,过滤掉 PDF 文件.

I just seperated it into two resources with filtering off for PDF file.

        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
                <include>**/*.txt</include>
                <include>**/*.html</include>
            </includes>
        </resource>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>false</filtering>
            <includes>
                <include>**/*.pdf</include>
            </includes>
        </resource>

感谢 Drew Buckley,我在尝试对文件进行二进制比较时遇到了问题.项目上的实际文件不同,从 maven 复制的目标文件夹中的文件也不同.

Thanks to Drew Buckley, I got the issue when trying to do a binary comparison of the file. Actual file on the project was different and the one on the target folder which gets copied from the maven was different.

现在可以正常使用了.

这篇关于使用 Java Files.copy 复制后出现空白页的 PDF 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 Java Files.copy 复制后出现空白页的 PDF 文件

基础教程推荐