How to download a pdf file diagrammatically from a web page with .html extension?

0 votes
I have reviewed ALL similar questions on this forum and have tried ALL of those methods however still was not able to programmatically download a test file

This is a test pdf file with an open access, so anybody can use it to test a download method.

How can I download this particular file so that it has a pdf extension?
Sep 28, 2018 in Selenium by Perry
• 17,020 points
97 views

1 answer to this question.

0 votes

For downloading a pdf file:

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();
    }
}
answered Sep 28, 2018 by Meci Matt
• 9,420 points

Related Questions In Selenium

0 votes
1 answer
0 votes
1 answer
+1 vote
1 answer
0 votes
1 answer

How to replace remote resources with local resource in a page with Selenium?

You could change URL for that script ...READ MORE

answered Aug 8, 2018 in Selenium by Samarpit
• 5,130 points
44 views
0 votes
1 answer

Finding WebDriver element with Class Name in java

The better way to handle this element ...READ MORE

answered Apr 10, 2018 in Selenium by nsv999
• 5,090 points
728 views
0 votes
1 answer
0 votes
1 answer

Geo-location microphone camera pop up

To Allow or Block the notification, access using Selenium and you have to ...READ MORE

answered May 11, 2018 in Selenium by Samarpit
• 5,130 points
984 views
0 votes
1 answer

How to use such xpath to find web elements

It's much simpler to use: #MyDiv123 > div.super or ...READ MORE

answered Jun 14, 2018 in Selenium by Samarpit
• 5,130 points
697 views
0 votes
1 answer
0 votes
1 answer

How to start with a POST request using Selenium?

No, you cannot do this. But you ...READ MORE

answered Jun 25, 2018 in Selenium by Meci Matt
• 9,420 points
2,061 views