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 - Finally, weโ€™ll talk about how ... 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....
Discussions

Is there a Java file download workflow that works with both web URLs and file URLs (file://)?

You could pass an InputStream instead of an url into your download method and pass that one into Channels.newChannel().

More on reddit.com
๐ŸŒ r/javahelp
8
2
February 25, 2020
How to download a file that gets processed over an HTTP request in Java?
I'm only getting the page data back from the request What does that mean? More on reddit.com
๐ŸŒ r/learnprogramming
11
1
April 18, 2021
Download file from google drive with URL
Please ensure that: Your code is properly formatted as code block - see the sidebar (About on mobile) for instructions You include any and all error messages in full You ask clear questions You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions. Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar If any of the above points is not met, your post can and will be removed without further warning. Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png ) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc. Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit. Code blocks look like this: public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World!"); } } You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above. If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures. To potential helpers Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice. I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns. More on reddit.com
๐ŸŒ r/javahelp
4
1
April 11, 2024
How to fetch data and download file from website in java?
You can use Javaโ€™s java.net package (specifically HttpURLConnection) or a third-party library like Jsoup (for scraping web pages) and Apache HttpClient (for making HTTP requests). If youโ€™re downloading files, java.nio.file and Files.copy() are handy. More on reddit.com
๐ŸŒ r/javahelp
12
1
February 5, 2025
๐ŸŒ
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. Here is the simple java download file from URL example program. It shows both ways to download file from URL in java.
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ efficient-api-interactions-with-java โ€บ lessons โ€บ downloading-files-from-an-api-using-javas-httpclient
Downloading Files from an API Using Java's HttpClient
Here's a basic example of downloading a file named welcome.txt from our API at http://localhost:8000/notes. This approach downloads the entire file at once, which is manageable for smaller files. This code sends a GET request and writes the response content to a local file. This method works well for small files but can strain memory for larger files. ... When dealing with large files, downloading them all at once can be inefficient and strain memory. To address this, Java's HttpClient allows handling the response as a stream using InputStream, which optimizes memory usage.
๐ŸŒ
Coderanch
coderanch.com โ€บ t โ€บ 417362 โ€บ java โ€บ Downloading-File-URL
Downloading File From URL (Java in General forum at Coderanch)
November 19, 2008 - Most of those sources are located on FTP servers that I access using the commons-net apache package. I currently have a resource, in this case a text file, that I need to access from a URL. If I enter the URL in my browser, I will get a download prompt to begin downloading this 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 - We use BufferedInputStream to read and FileOutputStream to write to the local file: private static void javaIO() { try { try (BufferedInputStream in = new BufferedInputStream(new URL(URL).openStream()); FileOutputStream fileOutputStream = new ...
๐ŸŒ
GitHub
gist.github.com โ€บ petehouston โ€บ 193b60b6b86194cdb53a83be12df6426
Download file using Java ยท GitHub
Clone via HTTPS Clone using the web URL. ... Clone this repository at <script src="https://gist.github.com/petehouston/193b60b6b86194cdb53a83be12df6426.js"></script> Save petehouston/193b60b6b86194cdb53a83be12df6426 to your computer and use it in GitHub Desktop. ... This is the source code demonstration for tutorial at https://kodemate.com/download-file-using-java/
Find elsewhere
๐ŸŒ
Smarterco
smarterco.de โ€บ java-download-file-from-url-to-temp-directory
Java: download file from URL to temp directory - smarterco.de
May 7, 2015 - package me.kamwo.examples.util; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.net.URLConnection; public class FileDownloader { /** * Returns a path to the downloaded file * * @param url * @param localFilename * @return * @throws IOException */ public static String downloadFromUrl(URL url, String localFilename) throws IOException { InputStream is = null; FileOutputStream fos = null; String tempDir = System.getProperty("java.io.tmpdir"); String outputPath = tempDir + "/" + localFilename; try { //connect URLConnection url
๐ŸŒ
GitHub
gist.github.com โ€บ docsallover โ€บ 13dac15388a9d4e50f9aaa25a00f1151
Downloading Files From URL's In Java ยท GitHub
Clone via HTTPS Clone using the web URL. ... Clone this repository at <script src="https://gist.github.com/docsallover/13dac15388a9d4e50f9aaa25a00f1151.js"></script> Save docsallover/13dac15388a9d4e50f9aaa25a00f1151 to your computer and use it in GitHub Desktop. ... This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below.
๐ŸŒ
MangoHost
mangohost.net โ€บ mangohost blog โ€บ how to download files using java url
How to Download Files Using Java URL
August 3, 2025 - Downloading files using Java URL is a fundamental skill every Java developer should master. Whether youโ€™re building automated backup systems, content aggregators, or data processing pipelines, knowing how to efficiently fetch remote files through Javaโ€™s built-in URL classes will save you countless hours. This guide walks through everything from ...
๐ŸŒ
Flexiple
flexiple.com โ€บ javascript โ€บ download-flle-using-javascript
How to Download a File Using JavaScript - Flexiple
JavaScript also needs to set the `download` attribute on the anchor tag, specifying the filename. After configuring the anchor tag, JavaScript adds this element to the document body.
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ jakarta ee โ€บ example of downloading file in a servlet
Example of Downloading File in a Servlet | Baeldung
August 22, 2022 - Some browsers will immediately download the file using the given filename and others will show a download dialog containing our predefined value. The exact action taken will depend on the browser. In the remaining lines of code, we take the ServletContext from the request, and use it to obtain the file at โ€œ/WEB-INF/sample.txtโ€.
๐ŸŒ
Tistory
father-lys.tistory.com โ€บ 115
[java] URL ํ˜ธ์ถœํ•ด์„œ ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ (InputStream)
June 8, 2024 - import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; public class FileDownload { public static void main(String[] args) { String fileURL = "https://example.com/path/to/file.txt"; // ๋‹ค์šด๋กœ๋“œํ•  ํŒŒ์ผ์˜ URL String saveDir = "/path/to/save/directory"; // ํŒŒ์ผ์„ ์ €์žฅํ•  ๋””๋ ‰ํ† ๋ฆฌ try { downloadFile(fileURL, saveDir); System.out.println("ํŒŒ์ผ ๋‹ค์šด๋กœ๋“œ ์™„๋ฃŒ."); } catch (IOException e) { System.out
๐ŸŒ
Aspose
products.aspose.com โ€บ html โ€บ java โ€บ save-file-from-url
Save File from URL โ€“ Aspose.HTML for Java
January 1, 2021 - 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. The url parameter defines the destination for the network request.
๐ŸŒ
Reddit
reddit.com โ€บ r/javahelp โ€บ is there a java file download workflow that works with both web urls and file urls (file://)?
r/javahelp on Reddit: Is there a Java file download workflow that works with both web URLs and file URLs (file://)?
February 25, 2020 -

The term project I came up with for a Java course revolves around being able to download files from the internet, but apparently AutoBot, the automated testing system that I have to pass, doesn't let you connect to the internet. The only solution I can think of is to use file URLs (for example, file://data/test/file.xml) in the test cases to bypass the network connection all together. However, the built-in method I'm using for downloads doesn't support file urls.

Here is one of my download functions:

public void download(File path, URL url) throws IOException {
    prepareDirectory(new File(path.getParent()));
    ReadableByteChannel channel = Channels.newChannel(url.openStream());
    FileOutputStream stream = new FileOutputStream(path);
    stream.getChannel().transferFrom(channel, 0, Long.MAX_VALUE);
}

But it throws "java.net.UnknownHostException: data" in the url.openStream() method call. I think the problem is that I need to generate an InputStream from a file URL.

Is there a downloading workflow that works with both http and file URLs? For a file url, I just want it to copy the file contents into the download destination, or anything really, as long as it accepts both kinds of URLs in one constructor or method call so I get 100% line/branch test coverage, and it writes the right data to the target path. I'm allowed to use third party open source libraries so something not in the standard library is also okay.

Failing that, how should I test something like this without an internet connection? My TAs don't even know for sure how to do this correctly.

It's ironic that my code actually works correctly, except when testing it due to an arbitrary limitation.

๐ŸŒ
DuckDB
duckdb.org โ€บ docs โ€บ current โ€บ clients โ€บ java
Java (JDBC) Client โ€“ DuckDB
For example, if your database is stored in /tmp/my_database, use the JDBC URL jdbc:duckdb:/tmp/my_database to create a connection to it. It is possible to open a DuckDB database file in read-only mode. This is for example useful if multiple Java processes want to read the same database file at the same time.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ javascript โ€บ how-to-trigger-a-file-download-when-clicking-an-html-button-or-javascript
How to Trigger a File Download when Clicking an HTML Button or JavaScript? - GeeksforGeeks
First create a textarea to capture ... downloadable file Now : Dynamically generate an anchor (<a>) tag using JavaScript's createElement property, setting attributes like download and href for file download....
Published ย  July 12, 2025
๐ŸŒ
Oracle
oracle.com โ€บ uk โ€บ java โ€บ technologies โ€บ downloads
Java Downloads | Oracle United Kingdom
Download the Java including the latest version 17 LTS on the Java SE Platform. These downloads can be used for any purpose, at no cost, under the Java SE binary code license.
๐ŸŒ
Website Downloader
websitedownloader.com โ€บ home โ€บ download file from url online
Download File from URL Online - WebsiteDownloader.com
August 23, 2023 - Click on the โ€˜Downloadโ€™ button. Wait for a few moments while our tool processes the file. Once the file is ready, click on the โ€˜Download Fileโ€™ button to save it to your device. Itโ€™s that simple! With WebsiteDownloader, you can download any file from a URL online in just a few clicks.