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

标签 java file pdf copy

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

这是它的代码:

    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页,但没有任何内容。

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

干杯, 马杜

最佳答案

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

这是我的 maven 资源:

        <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 文件上进行的,因此文件被复制为一个空的 doco 到目标文件夹。

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

        <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 复制的目标文件夹中的文件也不同。

现在可以正常使用了。

关于java - 使用 Java Files.copy 复制后出现空白页的 PDF 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20623501/

相关文章:

c++ - : Expected constructor,析构函数错误,或者 '.' token之前的类型转换 - 理解fstream

linux - SGID 子文件夹写入权限

c# - pdf呈现到asp.net网页导致静态标题

html - 用于打印媒体的 CSS3

java - 在 Heroku 上设置 PostgreSQL 数据库(需要 entityManagerFactory)

java - 跨平台发送大量数据的好方法是什么?

java - Android 字符串中的回车符/换行符\n

java - 如何分析在 cloud foundry 上运行的 spring boot webapp 的内存?

file - 大文件的 awk 和 sum 行

php - 在 PHP 中处理 PDF