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
🌐
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 - The most basic API we can use to download a file is Java IO. We can use the URL class to open a connection to the file we want to download. To effectively read the file, we’ll use the openStream() method to obtain an InputStream:
🌐
Stack Overflow
stackoverflow.com › questions › 58967558 › download-large-files-using-inputstream
java - Download large files using InputStream - Stack Overflow
Client client = Client.create(); WebResource webResource = client.resource(url.toString()); ClientResponse response = webResource.accept("application/json").get(ClientResponse.class); if (response.getStatus() != 200) { throw new RuntimeException("Failed : HTTP error code : "+ response.getStatus()); } String output = response.getEntity(String.class); byte[] fileContent = Base64.decodeBase64(output.getBytes()); if (fileContent != null) { OutputStream out = null; try { res.reset(); out = res.getOutputStream(); res.setContentType(contentType); res.setHeader("Content-Disposition", "inline; filename=" + fileName + "; size=" + String.valueOf(fileContent.length)); res.setContentLength(fileContent.length); out.write(fileContent); } catch (Exception ex) { e.printStackTrace(); } finally { out.flush(); out.close(); } } Since there were memory issues, I tried to take the InputStream approach.
🌐
codippa
codippa.com › home › download file from url in java
Java - Download file from a URL in 3 ways
January 12, 2025 - Java code to download file from URL with this method is given below. import java.net.URL; import java.net.URLConnection; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class FileDownloader { public static void main(String[] args) { OutputStream os = null; InputStream is = null; String fileUrl = "http://200.34.21.23:8080/app/file.txt"; String outputPath = "E:\\downloads\\downloaded.txt"; try { // create a url object URL url = new URL(fileUrl); // connection to the file URLConnection connection = url.openConnection();
🌐
amitph
amitph.com › home › java › how to download a file from url in java
How to Download a File from URL in Java - amitph
November 22, 2024 - Finally, we use the input stream from the HttpResponse and use File#copy() method to write it to a Path on disk. This section explains how to asynchronously download a file from a URL and save it to the disk.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-download-file-url
Java Download File from URL | DigitalOcean
August 4, 2022 - Today we will learn how to download a file from URL in java. We can use java.net.URL openStream() method to download file from URL in java program. We can use Java NIO Channels or Java IO InputStream to read data from the URL open stream and then save it to file.
🌐
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 - When the returned value is -1, you can use the function copyLarge(inputStream, fileOS) in place of the copy(inputstream, fileOS) function to handle this load. Both of these functions buffer the inputstream internally. The internal buffer means we do not have to use the BufferedInputStream class to enhance our code performance and helps us avoid writing boilerplate code. Another library managed by the Apache organization is the HttpComponents package. This library uses the request-response mechanism to download the file from a given URL.
🌐
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 javaNIO2() ...mmons-io</artifactId> <version>2.11.0</version> </dependency> We use FileUtils.copyURLToFile method to download: private static void apacheCommonIO() { try { URL website = new URL(URL); ...
Find elsewhere
🌐
GeeksforGeeks
geeksforgeeks.org › java › implement-how-to-load-file-as-inputstream-in-java
Implement how to load File as InputStream in Java - GeeksforGeeks
July 23, 2025 - The read() method of InputStream class, reads a byte of data from the input stream. The next byte of data is returned, or -1 if the end of the file is reached and throws an exception if an I/O error occurs.
🌐
STechies
stechies.com › download-file-from-url-java
How to Download a File from a URL in Java
Here, we have to import the BufferInputStream, FileInputStream, InputStream, IOException, and java.net.URL. Now, create a Main class with a method URLDnldFile() that throws IO exception. The function uses two parameters, one URL link and the other file name. Create a variable ‘d’ of type byte.
🌐
CodeJava
codejava.net › java-se › networking › use-httpurlconnection-to-download-file-from-an-http-url
Java HttpURLConnection to download file from an HTTP URL
July 18, 2019 - package net.codejava.networking; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; /** * A utility that downloads a file from a URL.
🌐
Mkyong
mkyong.com › home › java › how to convert inputstream to file in java
How to convert InputStream to File in Java - Mkyong.com
December 27, 2020 - package com.mkyong.io.howto; import ... } } } We can use FileInputStream to convert a File to an InputStream. File file = new File("d:\\download\\google.txt"); InputStream inputStream = new FileInputStream(file);...
🌐
Baeldung
baeldung.com › home › java › java io › java – write an inputstream to a file
Java - Write an InputStream to a File | Baeldung
August 20, 2025 - How to convert an InputStream to a byte[] using plain Java, Guava or Commons IO. ... @Test public void whenConvertingToFile_thenCorrect() throws IOException { Path path = Paths.get("src/test/resources/sample.txt"); byte[] buffer = java.nio.file.Files.readAllBytes(path); File targetFile = new File("src/test/resources/targetFile.tmp"); OutputStream outStream = new FileOutputStream(targetFile); outStream.write(buffer); IOUtils.closeQuietly(outStream); }
🌐
GitHub
github.com › box › box-android-sdk › issues › 53
Add method getInputStream(String fileId) to file download methods. · Issue #53 · box/box-android-sdk
November 24, 2015 - InputStream getInputStream(String fileId) { return response.getHttpURLConnection().getInputStream(); } to public API? By the way, you already have presented such method in box-java-sdk: URL url = new URL("files/" + file.getID() + "/content") BoxAPIRequest request = new BoxAPIRequest(api, url, "GET"); BoxAPIResponse response = request.send(); InputStream bodyStream = response.getBody(); No one assigned ·
Author   box
🌐
Mkyong
mkyong.com › home › java › how to download file from website- java / jsp
How to download file from website- Java / Jsp - Mkyong.com
April 15, 2010 - File file = new File("C:\\temp\\downloadfilename.csv"); FileInputStream fileIn = new FileInputStream(file); ServletOutputStream out = response.getOutputStream(); byte[] outputByte = new byte[4096]; //copy binary contect to output stream while(fileIn.read(outputByte, 0, 4096) != -1) { out.write(outputByte, 0, 4096); } fileIn.close(); out.flush(); out.close(); Export database data or string directly to InputStream for user download.
🌐
Vogella
vogella.com › tutorials › JavaNetworking › article.html
Java Networking - Using HttpURLConnection to download files from the Internet - Tutorial
package com.vogella.java.introduction; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class DownloadWebpageExample { private static final String newLine = System.getProperty("line.separator"); public static void main(String[] args) { try { URL url = new URL("https://www.vogella.com/"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); String readStream = readStream(con.getInputStream()); // Give output for the command line System.out.println(r
🌐
Java
download.java.net › java › early_access › valhalla › docs › api › java.base › java › io › InputStream.html
InputStream (Java SE 23 & JDK 23 [build 1])
The skip method may, for a variety of reasons, end up skipping over some smaller number of bytes, possibly 0. This may result from any of a number of conditions; reaching end of file before n bytes have been skipped is only one possibility. The actual number of bytes skipped is returned. If n is negative, the skip method for class InputStream always returns 0, and no bytes are skipped.