Save File from URL using Java

Contents
[ Hide Show ]

This article presents a Java example that can be used to save a file from a URL. It demonstrates how to create a request, download a file using Aspose.HTML for Java, and save it to a local file system. This approach is useful for automating the download of files from the web for offline access, archiving, or further processing.

How to Save File from URL

In order to save file from URL, you need to know its URL and have a network operations handler suitable for its protocol. Aspose.HTML for Java library provides you with convenient functionality for processing URLs with different kinds of protocols. In order to use it, you just need to create an empty HTML document and call the network request handler, as shown in the following Java example:

  1. Use the HTMLDocument() constructor to create a blank instance of the HTMLDocument class. This is necessary to gain access to the network functionality through the document’s context.
  2. Create a Url object and pass the path of the file you want to download.
  3. Use the RequestMessage(url) constructor to create a request message for the given URL. The url parameter is passed to the constructor, specifying the URL to which the request will be sent.
  4. Use the document.getContext().getNetwork().send(request) method to send the download request and receive a response.
  5. Check if the response was successful using the isSuccess() method. If successful, extract the file name from the URL path.
  6. Finally, use the FileHelper.writeAllBytes() method to save the file to your local file system.

This code lets you programmatically download and save any file from a URL:

 1// Create a blank document; it is required to access the network operations functionality
 2final HTMLDocument document = new HTMLDocument();
 3
 4// Create a URL with the path to the resource you want to download
 5Url url = new Url("https://docs.aspose.com/html/net/message-handlers/message-handlers.png");
 6
 7// Create a file request message
 8final RequestMessage request = new RequestMessage(url);
 9
10// Download file from URL
11final ResponseMessage response = document.getContext().getNetwork().send(request);
12
13// Check whether response is successful
14if (response.isSuccess()) {
15    String[] split = url.getPathname().split("/");
16    String path = split[split.length - 1];
17
18    // Save file to a local file system
19    FileHelper.writeAllBytes(path, response.getContent().readAsByteArray());
20}
Example_SaveFileFromURL hosted with ❤ by GitHub

Aspose.HTML provides a set of free online HTML Web Applications, including converters, mergers, SEO tools, HTML code generators, URL utilities, and more. These browser-based tools work on all operating systems and don’t require any additional software installation. Whether you need to convert or merge files, extract web data, generate HTML code, or analyze pages for SEO, you can do it all right on the web. Streamline your daily tasks and increase your productivity with our easy-to-use HTML Web Apps – anytime, anywhere.

Text “HTML Web Applications”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.