Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Java opens URL to save file to specific folder

I’m trying to code based on the manual operation. For manual, I have a URL and when I paste the URL to the Chrome browser, the browser automatically downloads the PDF file from that URL and save to folder "download" without prompting any user input. With Code, I’m able to accomplish the same thing as the manual operation. However I would like the code to save the PDF into specific folder instead of default folder "download". Is it possible to do that?

    public static void browseURL() {
    try {
            
        String url ="mycompanyURL";
        System.out.println("url " + url );
        
        Desktop desktop = Desktop.getDesktop();
        URI uri = new URI (url);            
        desktop.browse(uri);
        
        
    }catch(Exception err) {

        System.out.println("exception " + err.getMessage());
    }
  }

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

When I had to do that in old versions of Java, I used the following snippet (pure Java, source: Baeldung).

public void streamFromUrl(String downloadUrl, String filePath) throws IOException {
    File file = new File(filePath);
    try (BufferedInputStream in = new BufferedInputStream(new URL(downloadUrl).openStream());
         FileOutputStream fileOutputStream = new FileOutputStream(file)) {
        byte[] dataBuffer = new byte[1024];
        int bytesRead;
        while ((bytesRead = in.read(dataBuffer, 0, 1024)) != -1) {
            fileOutputStream.write(dataBuffer, 0, bytesRead);
        }
    }
}

The above opens an input stream on the URL, and outputs the bytes of such stream into a file output stream (where the file is wherever you wish).

Alternatively, there are many libraries doing that in one/two liners (the article I posted shows some of those alternatives).

Also, starting from more recent versions of Java, there are other shorter options:

public void streamFromUrl(String downloadUrl, String filePath) throws IOException {
    try (InputStream in = new URL(downloadUrl).openStream()) {
        Files.copy(in, Paths.get(new File(filePath)), StandardCopyOption.REPLACE_EXISTING);
    }
}

Depending on the version of Java you have, you may pick one of those. Generally speaking, I suggest you reading through the Baeldung’s article and check the one that best suits for you.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading