Give Java NIO a try:

URL website = new URL("http://www.website.com/information.asp");
ReadableByteChannel rbc = Channels.newChannel(website.openStream());
FileOutputStream fos = new FileOutputStream("information.html");
fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE);

Using transferFrom() is potentially much more efficient than a simple loop that reads from the source channel and writes to this channel. Many operating systems can transfer bytes directly from the source channel into the filesystem cache without actually copying them.

Check more about it here.

Note: The third parameter in transferFrom is the maximum number of bytes to transfer. Integer.MAX_VALUE will transfer at most 2^31 bytes, Long.MAX_VALUE will allow at most 2^63 bytes (larger than any file in existence).

Answer from dfa on Stack Overflow
🌐
STechies
stechies.com › download-file-from-url-java
How to Download a File from a URL in Java
import java.io.BufferedInputStream; ...println("Call this method when you want your application to have this."); //Call the URLDnldFile() method }}...
🌐
Baeldung
baeldung.com › home › java › java io › download a file from an url in java
Download a File From an URL in Java | Baeldung
January 8, 2024 - Instead of accumulating each HttpResponseBodyPart into memory, we use a FileChannel to write the bytes to our local file directly. We’ll use the getBodyByteBuffer() method to access the body part content through a ByteBuffer. ByteBuffers have the advantage that the memory is allocated outside of the JVM heap, so it doesn’t affect our application memory. Another highly used library for IO operation is Apache Commons IO. We can see from the Javadoc that there’s a utility class named FileUtils that we use for general file manipulation tasks.
🌐
GitHub
gist.github.com › madan712 › 810b05f3d2073bad6c16
Java program to upload/download files from remote server · GitHub
I tried to use the above approach to download from server to my local machine(client location) but it creates a new file at JAR location(i.e at Server location) instead of client location. The code snippet is as below & this creates a file at JAR location with file name C:\Users\PFName-CFName_Co.xlsx Please check & help me on how to download the file to client location(anywhere in c drive) String SerDir=System.getProperty("user.home") + "/Desktop/"; String LocDir="C:\Users\"; String SerFC = SerDir+PFName+"-"+CFName+"_Co.xlsx"; String LocFC = LocDir+PFName+"-"+CFName+"_Cos.xlsx"; Session sessio
🌐
codippa
codippa.com › home › download file from url in java
Java - Download file from a URL in 3 ways
January 12, 2025 - This method takes two arguments: 1. java.net.URL object pointing to the source file, and 2. java.io.File object which points to an output file path. Remember that both paths should contain the name of file at the end and output path should be ...
🌐
RoseIndia
roseindia.net › java › example › java › io › file-url-download.shtml
URL file Download and Save in the Local Directory
This program specifies the directory path where the files to be stored as first command line argument and second command line arguments specifies URL File to be stored. Java creates link between Url and Java application.Using the openConnection() method creates URLConnection object.
🌐
Delft Stack
delftstack.com › home › howto › java › java downloading file
How to Download File in Java | Delft Stack
February 2, 2024 - At last, we call the copyURLToFile() method from the FileUtils class that takes two arguments: the URL object and the file object. import java.io.File; import java.io.IOException; import java.net.URL; import org.apache.commons.io.FileUtils; public class DownloadFile { public static void main(String[] args) throws IOException { URL fetchWebsite = new URL( "https://theswissbay.ch/pdf/Gentoomen Library/Programming/Java/Introduction to Java IO.pdf"); File file = new File("JavaIo.pdf"); FileUtils.copyURLToFile(fetchWebsite, file); } }
Find elsewhere
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-download-file-url
Java Download File from URL | DigitalOcean
August 4, 2022 - We can use Java NIO Channels or Java IO InputStream to read data from the URL open stream and then save it to file.
🌐
Medium
pkslow.medium.com › multiple-ways-to-download-files-from-internet-in-java-ced5d3aaae2c
Multiple Ways to Download Files from Internet in Java | by Larry Deng | Medium
April 3, 2023 - private static void javaNIO1() { try { URL website = new URL(URL); ReadableByteChannel rbc = Channels.newChannel(website.openStream()); FileOutputStream fos = new FileOutputStream("pkslow.nio.html"); fos.getChannel().transferFrom(rbc, 0, ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › file-download-manager-system-in-java
File Download Manager System in Java - GeeksforGeeks
June 10, 2025 - Let's now try to implement the code. This class is used to represents an individual download task and also handles the actual file downloading process. It implements the Callable interface to allow execution of tasks using threads. This class is also responsible for handling the download logic for each file, such as starting the download, simulating download progress, and returning the status of the download. ... package com.downloader; import java.util.concurrent.Callable; public class DownloadTask implements Callable<String> { private String fileUrl; private String destination; public Downlo
🌐
Matjazcerkvenik
matjazcerkvenik.si › developer › java-download-file-via-http.php
Java Download File via HTTP - Matjaž Cerkvenik
// url=http://www.example.com/testFile.zip // localFile=/path/to/testFile.zip public static void download(String url, String localFile) throws Exception { System.out.println("Downloading " + localFile); URL website = new URL(url); URLConnection connection = website.openConnection(); ...
🌐
Attacomsian
attacomsian.com › blog › java-download-save-file-from-internet
How to download and save a file from Internet in Java
December 11, 2019 - try { // internet URL URL url = ... readTimeout = 300 * 1000; // 3 min // download and save file FileUtils.copyURLToFile(url, file, connectionTimeout, readTimeout); } catch (IOException e) { e.printStackTrace(); }...
🌐
Stack Abuse
stackabuse.com › how-to-download-a-file-from-a-url-in-java
How to Download a File from a URL in Java
August 21, 2018 - The most easily available and a basic package available for downloading a file from internet using Java code is the Java IO package. Here we will be using the BufferedInputStream and the URL classes to open and read a file on a given address to a file on our local system.
🌐
TutorialKart
tutorialkart.com › java › java-download-file-from-url
How to Download File from URL in Java?
January 10, 2023 - To download file from URL in Java, you can use FileUtils.copyURLToFile() method of org.apache.commons.io package. In this tutorial, we have examples to download HTML file, PNG file, etc.
🌐
javaspring
javaspring.net › blog › java-file-download
Java File Download: A Comprehensive Guide — javaspring.net
Java provides multiple ways to implement file download operations, whether it's for local file copying or downloading from a remote server via HTTP. By understanding the fundamental concepts, usage methods, common practices, and best practices, developers can create efficient and robust file-downloading applications.
🌐
Javatpoint
javatpoint.com › example-of-downloading-file-from-the-server-in-servlet
Example of downloading file from the server in servlet - javatpoint
Example of downloading file from the server in servlet with servlets, hidden, form, field, java, tutorial, examples, http, client, server, session, cookies, file upload, download servlet etc.
🌐
GitHub
gist.github.com › petehouston › 193b60b6b86194cdb53a83be12df6426
Download file using Java · GitHub
This is the source code demonstration for tutorial at https://kodemate.com/download-file-using-java/
🌐
DZone
dzone.com › coding › java › java: how to save / download a file available at a particular url location on the internet?
Java: How to Save / Download a File Available at a Particular URL Location on the Internet?
January 3, 2012 - package singz.test; import java.io.BufferedInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; import org.apache.commons.io.FileUtils; /* * @author Singaram Subramanian */ public class FileDownloadTest { public static void main(String[] args) { // Make sure that this directory exists String dirName = "C:\\FileDownload"; try { System.out.println("Downloading \'Maven, Eclipse and OSGi working together\' PDF document..."); saveFileFromUrlWithJavaIO( dirName + "\\maven_eclipse_and_osgi_working_to