How to Download Files from URL
The ability to download file from URL is important for various applications such as web scraping and content analysis. Aspose.HTML for Java is a robust library that simplifies this process by offering developers a set of tools to navigate and gather information from HTML documents seamlessly. Let’s explore how to save file from URL with Java.
Save File from URL Using Java
Using Aspose.HTML for Java library, you can easily create your own application, since our API provides a powerful toolset to analyze and collect information from HTML documents. If you want to use HTML data scraping features in your product or programmatically download file from URL, see the code example below:
Java code to download file from URL
// Create a blank document; it is required to access the network operations functionality
final HTMLDocument document = new HTMLDocument();
// Create a URL with the path to the resource you want to download
Url url = new Url("https://docs.aspose.com/html/net/message-handlers/message-handlers.png");
// Create a file request message
final RequestMessage request = new RequestMessage(url);
// Download file from URL
final ResponseMessage response = document.getContext().getNetwork().send(request);
// Check whether response is successful
if (response.isSuccess()) {
String[] split = url.getPathname().split("/");
String path = split[split.length - 1];
// Save file to a local file system
FileHelper.writeAllBytes(path, response.getContent().readAsByteArray());
}
Steps to Save File from URL
- Use the HTMLDocument() constructor to create an empty instance of the HTMLDocument class. This step is required to enable network access within the context of the document.
- Create a
Url
object and pass the path of the file you want to download. - Use the
RequestMessage(
url
) constructor to create a request message targeting the specified URL. Theurl
parameter defines the destination for the network request. - Send the request and receive the response from the specified URL. If the request is successful, extract the file name from the URL path.
- Use
FileHelper.writeAllBytes()
to save the file to your local file system.
Downloading files from URLs can be helpful for offline access when your internet connection is limited, for collaboration and sharing content, for archiving and backing up to prevent data loss, or simply for storing essential resources, such as documents, images, videos, or audio files, for future use. It is also a useful method for remote access to educational materials, allowing you to study anytime and anywhere – even while commuting or traveling.
With Aspose.HTML for Java, you can easily create a tool that parses a web page and downloads files that you need. To learn more about how to programmatically save files from URLs using Java, refer to the documentation article Save File from URL using Java .
Note: It is important to respect copyright laws and obtain the proper permissions or licenses before using saved files for commercial purposes. We do not support the extraction and use of other people’s files for commercial purposes without their consent.
Get Started with Aspose.HTML for Java Library
Aspose.HTML for Java is an advanced web scraping and HTML parsing library. One can create, edit, navigate through nodes, extract data and convert HTML, XHTML, and MHTML files to PDF, Images, and other formats. Moreover, it also handles CSS, HTML Canvas, SVG, XPath, and JavaScript out-of-the-box to extend manipulation tasks. It’s a standalone API and does not require any software installation.
You can download its latest version directly from
Aspose Maven Repository
and install it within your Maven-based project by adding the following configurations to the pom.xml.
Repository
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>https://repository.aspose.com/repo/</url>
</repository>
Dependency
<dependency>
<groupId>com.aspose</groupId>
<artifactId>aspose-html</artifactId>
<version>version of aspose-html API</version>
<classifier>jdk17</classifier>
</dependency>
Other Supported Features
Use the Aspose.HTML for Java library to parse and manipulate HTML-based documents. Clear, safe and simple!