In this post, I will guide you how to write Java code to download files from web server programmatically.

You know, in Java, we can use the classes URLand HttpURLConnection in the package java.net to programmatically download a file from a given URL by following these steps:

    • Create a URL object for a given URL. The URL can be either:
      • A direct link which contains the real file name at the end, for example:

http://jdbc.postgresql.org/download/postgresql-9.2-1002.jdbc4.jar

 

      • An indirect link which does not contain the real file name, for example:

http://myserver.com/download?id=1234

    • Open connection on the URL object – which would return an HttpURLConnection object if the URL is an HTTP URL.
    • Open the input stream of the opened connection.
    • Create an output stream to save file to disk.
    • Repeatedly read array of bytes from the input stream and write them to the output stream, until the input stream is empty.
    • Close the input stream, the output stream and the connection.
For the purpose of reusability, we create a utility class as follows:

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.
 * @author www.codejava.net
 *
 */
public class HttpDownloadUtility {
	private static final int BUFFER_SIZE = 4096;

	/**
	 * Downloads a file from a URL
	 * @param fileURL HTTP URL of the file to be downloaded
	 * @param saveDir path of the directory to save the file
	 * @throws IOException
	 */
	public static void downloadFile(String fileURL, String saveDir)
			throws IOException {
		URL url = new URL(fileURL);
		HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
		int responseCode = httpConn.getResponseCode();

		// always check HTTP response code first
		if (responseCode == HttpURLConnection.HTTP_OK) {
			String fileName = "";
			String disposition = httpConn.getHeaderField("Content-Disposition");
			String contentType = httpConn.getContentType();
			int contentLength = httpConn.getContentLength();

			if (disposition != null) {
				// extracts file name from header field
				int index = disposition.indexOf("filename=");
				if (index > 0) {
					fileName = disposition.substring(index + 10,
							disposition.length() - 1);
				}
			} else {
				// extracts file name from URL
				fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
						fileURL.length());
			}

			System.out.println("Content-Type = " + contentType);
			System.out.println("Content-Disposition = " + disposition);
			System.out.println("Content-Length = " + contentLength);
			System.out.println("fileName = " + fileName);

			// opens input stream from the HTTP connection
			InputStream inputStream = httpConn.getInputStream();
			String saveFilePath = saveDir + File.separator + fileName;
			
			// opens an output stream to save into file
			FileOutputStream outputStream = new FileOutputStream(saveFilePath);

			int bytesRead = -1;
			byte[] buffer = new byte[BUFFER_SIZE];
			while ((bytesRead = inputStream.read(buffer)) != -1) {
				outputStream.write(buffer, 0, bytesRead);
			}

			outputStream.close();
			inputStream.close();

			System.out.println("File downloaded");
		} else {
			System.out.println("No file to download. Server replied HTTP code: " + responseCode);
		}
		httpConn.disconnect();
	}
}
Note that in the static method downloadFile(), we have to check HTTP response code from the server to make sure the URL is available (HTTP status 200). Then we extract the file name either from the HTTP header Content-Disposition (in case the URL is an indirect link), or from the URL itself (in case the URL is the direct link). We also print out some debugging information like Content-Type, Content-Disposition, Content-Length and file name.

And here is a test program which employs the utility class above:

package net.codejava.networking;

import java.io.IOException;

public class HttpDownloader {

	public static void main(String[] args) {
		String fileURL = "http://jdbc.postgresql.org/download/postgresql-9.2-1002.jdbc4.jar";
		String saveDir = "E:/Download";
		try {
			HttpDownloadUtility.downloadFile(fileURL, saveDir);
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}
This program downloads the file postgresql-9.2-1002.jdbc4.jar and saves it into the directory E:/Download. It would produce the following output:

Content-Type = application/java-archive
Content-Disposition = null
Content-Length = 579785
fileName = postgresql-9.2-1002.jdbc4.jar
File downloaded
 

Watch this video tutorial to see how to build, run and test this program:



 

Related Java File Download Tutorials:

 

Other Java network tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Attachments:
Download this file (HttpDownloader.java)HttpDownloader.java[ ]0.3 kB
Download this file (HttpDownloadUtility.java)HttpDownloadUtility.java[ ]2 kB

Add comment

   


Comments 

#48majo2021-08-28 06:29
Thank you... this works great.

There is a small bug though. Where you parse content-disposition, it should be

if (index != -1 )

instead of if (index > 0) {
Quote
#47Xenu2021-04-12 10:01
Rly nice man, thanks:>
Quote
#46Prajakta2021-02-24 00:14
I am trying to download an .xlsx file from an url using the similar code. but I am getting corrupted file downloaded.
Quote
#45Mendes2020-11-08 10:52
Until one month ago, I could download the URL cdn.newseum.org/dfp/jpg11/lg/GER_DT.jpg
(11 is the day, use today number) using URLConnection or HttpURLConnection. Now it does not work but I can download manually via Google Chrome. Why?
Quote
#44jeevith2020-07-07 06:01
I get a file not found exception using this code then I surrounded it with try-catch but did not work, so how to solve this??
Quote