PDF file with empty pages appearing after Copying using Java Files.copy

淺唱寂寞╮ 提交于 2019-12-23 01:36:09

问题


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

Here is the code for it:

    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 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.

Cheers, Madhu


回答1:


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

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>

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

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>

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.

It works fine now.




回答2:


Yes, this worked for me too; in Maven documentation i found this for more information;

Warning: Do not filter files with binary content like images! This will most likely result in corrupt output. If you have both text files and binary files as resources, you need to declare two mutually exclusive resource sets. The first resource set defines the files to be filtered and the other resource set defines the files to copy unaltered



来源:https://stackoverflow.com/questions/20623501/pdf-file-with-empty-pages-appearing-after-copying-using-java-files-copy

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!