Save File from a URL in Java

Aspose.HTML for Java provides a network service that an application can use to request a remote resource and save the response body locally. This is useful when downloading a resource is part of a broader HTML loading, inspection, or data extraction workflow.

To download a file from a URL, create a RequestMessage, send it through the network service available from an HTMLDocument context, check ResponseMessage.isSuccess(), and write the response bytes to a local file.

Download a File from a URL in Java

The example uses an empty HTMLDocument to access its network service. It then downloads an image, derives the output name from the URL path, and writes the response body to the local file system.

  1. Create an empty HTMLDocument to access the document context and network service.
  2. Create a Url for the remote resource.
  3. Initialize a RequestMessage with that URL.
  4. Send the request and receive a ResponseMessage.
  5. Continue only when response.isSuccess() returns true.
  6. Derive a file name from the URL path and write the response bytes to the required destination.
 1// Download file from URL using Java
 2
 3// Create a blank document; it is required to access the network operations functionality
 4final HTMLDocument document = new HTMLDocument();
 5
 6// Create a URL with the path to the resource you want to download
 7Url url = new Url("https://docs.aspose.com/html/net/message-handlers/message-handlers.png");
 8
 9// Create a file request message
10final RequestMessage request = new RequestMessage(url);
11
12// Download file from URL
13final ResponseMessage response = document.getContext().getNetwork().send(request);
14
15// Check whether response is successful
16if (response.isSuccess()) {
17    String[] split = url.getPathname().split("/");
18    String path = split[split.length - 1];
19
20    // Save file to a local file system
21    FileHelper.writeAllBytes(path, response.getContent().readAsByteArray());
22}

The example calls readAsByteArray(), so the complete response is held in memory before it is written. Use this pattern only when the downloaded file comfortably fits in the memory available to the application.

What File Types Can Be Downloaded?

The network request returns response data rather than converting its format. The same workflow can save an image, PDF, archive, stylesheet, or another accessible resource. The file extension should match the actual response content; deriving it from the URL alone does not validate the media type.

Aspose.HTML is not required for an isolated general-purpose HTTP download. Standard Java networking APIs may be simpler when no HTML document processing or Aspose.HTML network context is involved.

Common File Download Issues

IssueLikely causeFix
No file is createdThe response was not successful, and the code enters the if block only for a successful response.Inspect the response status and handle the failure explicitly.
The output name is emptyThe URL path ends with / or does not contain a file name.Provide an explicit output name or obtain one from trusted response metadata.
The saved file contains HTML instead of the expected dataThe server returned an error or sign-in page with a successful transport response.Validate the content type and, when appropriate, the response body before saving.
A large download consumes too much memoryreadAsByteArray() buffers the complete response.Use a streaming download workflow suitable for large resources.

FAQ

Can this example download a PDF or image?

Yes. It saves the raw response bytes and does not depend on a particular file format. Ensure that the URL returns the expected content and choose the correct output extension.

Does this example convert the downloaded file?

No. It downloads and saves the response body without converting it. Use the appropriate Aspose.HTML conversion workflow when the source must be transformed into another format.

Related Aspose.HTML Articles

Other Platforms

Try HTML Web Applications

Aspose.HTML provides free online HTML Web Applications for converting files, extracting web data, generating HTML, and analyzing pages without installing additional software.

Text “HTML Web Applications”