如何从具有.html扩展名的网页中以编程方式下载pdf文件? [英] How to download a pdf file programmatically from a webpage with .html extension?

查看:108
本文介绍了如何从具有.html扩展名的网页中以编程方式下载pdf文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已审查了所有类似的问题(不仅如此!)这个论坛并尝试了所有这些方法,但是仍然无法以编程方式下载测试文件:

I have reviewed ALL similar questions (not only this!) on this forum and have tried ALL of those methods however still was not able to programmatically download a test file: http://pdfobject.com/markup/examples/full-browser-window.html

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

The following is the direct link to the test file that i am trying to download. This is a test pdf file with an open access, so anybody can use it to test a download method.

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

推荐答案

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

For downloading a file, perhaps you could try something like this:

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();
    }
}

这篇关于如何从具有.html扩展名的网页中以编程方式下载pdf文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆