Download file from HTTP & HTTPS server using Java

In the earlier articles, JavaBeat has published many articles on uploading and downloding of files using the Java programming. Upload and Download is essential utility in the programming world because every server must have the feature to upload and download the files by the user or downlod the reports incase of the enterprise applications. The challenging part is that, the process could be different based on the type of server and the security infrastructure in that serever for example SSL certificate. This example provides the simple way to download a file from the HTTP web server and store it in your local system. Also there is a way to download a file from the HTTPS server.

also read:

Try this program and let us know if it is working for you. If you have any doubts, please psot it in the comments section. We would try to resolve your problems.

 package com.service;

import java.nio.charset.MalformedInputException;

public class TestClass {
	public static void main(String[] args) {
		URL url = null;
		URLConnection con = null;
		int i;
		try {
			url = new URL("https://localhost:8080/AppName/FileName.txt");
			con = url.openConnection();
			File file = new File(
			BufferedInputStream bis = new BufferedInputStream(
			BufferedOutputStream bos = new BufferedOutputStream(
					new FileOutputStream(file.getName()));
			while ((i = != -1) {
		} catch (MalformedInputException malformedInputException) {
		} catch (IOException ioException) {

The above code will work for the normal HTTP server. Incase if you are trying to download from the HTTPS server, you will get the follwoing exception (<strong>SSLHandshakeException</strong>: Remote host closed connection during handshake
	at Source)
	at Source)
	at Source)
	at Source)
	at Source)
	at Source)
	at Source)
	at Source)
Caused by: SSL peer shut down incorrectly
	at Source)
	... 9 more

You have to use the following snippet of code to make it work:




About Mohamed Sanaulla

In his day job he works on developing enterprise applications using ADF. He is also the moderator of JavaRanch forums and an avid blogger.

  • sanket

    Hi.. I have tried this code. It works fine if i am using it as a normal java application but when it try to use the same code in servlet i am not able to download the help.
    Can u please tell what changes would i need to do.

    Thanx in advance..

    • suthukrish

      HI Sankat,

      Thank you for the comments. It is the standalone program. This code can be inegrated to the servlet with the little modification. You can retain the download code, and add the response type as the file type which you are downloading, that will prompt for downloading the file.

      Is it your doubt?


  • Najm

    Hi… i want to use this code, just a few clarifications. Suppose i have a file in there is a hyper link where i need to give the link what should be given in this?

  • britto

    Need to upload files to https server in android using php

  • jaisathiya

    thanks for posting this code to download file from the url.for example if i have link https://127..0.0.1/test.jsp .if i use this link in browser it willl a test.txt file.if i need to download the same in java apps how do i do?