繁体   English   中英

如何从具有.html扩展名的网页以编程方式下载pdf文件?

[英]How to download a pdf file programmatically from a webpage with .html extension?

我在这个论坛上已经审查了所有类似的问题 (不仅是这个!),并尝试了所有这些方法,但是仍然无法以编程方式下载测试文件: http : //pdfobject.com/markup/examples/full-browser- window.html

以下是我要下载的测试文件的直接链接 这是一个具有开放访问权限的测试pdf文件,因此任何人都可以使用它来测试下载方法。

如何下载此特定文件,使其具有pdf扩展名?

要下载文件,也许您可​​以尝试执行以下操作:

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

public final class FileDownloader {

    private FileDownloader(){}

    public static void main(String args[]) throws IOException{
        download("http://pdfobject.com/pdf/sample.pdf", new File("sample.pdf"));
    }

    public static void download(final String url, final File destination) throws IOException {
        final URLConnection connection = new URL(url).openConnection();
        connection.setConnectTimeout(60000);
        connection.setReadTimeout(60000);
        connection.addRequestProperty("User-Agent", "Mozilla/5.0");
        final FileOutputStream output = new FileOutputStream(destination, false);
        final byte[] buffer = new byte[2048];
        int read;
        final InputStream input = connection.getInputStream();
        while((read = input.read(buffer)) > -1)
            output.write(buffer, 0, read);
        output.flush();
        output.close();
        input.close();
    }
}

让我给您一个简短的解决方案,它带有一个名为JSoup的库, BalusC经常在他的答案中使用该库。

//Get the response
Response response=Jsoup.connect(location).ignoreContentType(true).execute();

//Save the file 
FileOutputStream out = new FileOutputStream(new File(outputFolder + name));
out.write(response.bodyAsBytes());
out.close();

好吧,您现在必须已经猜到了,pdf就是其中的response.body() 您可以使用这段代码下载任何二进制文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM