Unable to download pdf weblinks using org.apache.commons.io java library

巧了我就是萌 提交于 2019-12-12 03:52:53

问题


I am trying to download PDF weblinks using org.apache.commons.io library java. I want download 'https://----------------.pdf' intp pdf file, when I run following java class it creates the destination pdf on my disk but I am unable to open or read that pdf. It throws an error file is corrupted or unable to read.

I would appreciate if somebody will check whats wrong in my code and how to download pdf weblinks in to pdf file.

import java.io.File;
import java.io.IOException;
import java.net.URL;

import org.apache.commons.io.FileUtils;

public class Simple {
    public static void main(String[] args) {
        try {
            URL url = new URL("https://docs.google.com/**********.pdf");
            File destination = new File("kodejava.html");

            //
            // Copy bytes from the URL to the destination file.
            //
            FileUtils.copyURLToFile(url, destination);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

回答1:


You need to save your file as a pdf, not an html.Try renaming the downloaded file as kodejava.pdf and open. I am able to download it as .pdf and open it.



来源:https://stackoverflow.com/questions/19467172/unable-to-download-pdf-weblinks-using-org-apache-commons-io-java-library

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