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.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.net.URLConnection;
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(
					"C:\Foldername\Address.txt");
			BufferedInputStream bis = new BufferedInputStream(
					con.getInputStream());
			BufferedOutputStream bos = new BufferedOutputStream(
					new FileOutputStream(file.getName()));
			while ((i = bis.read()) != -1) {
				bos.write(i);
			}
			bos.flush();
			bis.close();
		} catch (MalformedInputException malformedInputException) {
			malformedInputException.printStackTrace();
		} catch (IOException ioException) {
			ioException.printStackTrace();
		}
	}
}

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 (javax.net.ssl.SSLHandshakeException):

 javax.net.ssl.<strong>SSLHandshakeException</strong>: Remote host closed connection during handshake
	at com.sun.net.ssl.internal.ssl.SSLSocketImpl.readRecord(Unknown Source)
	at com.sun.net.ssl.internal.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
	at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
	at com.sun.net.ssl.internal.ssl.SSLSocketImpl.startHandshake(Unknown Source)
	at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source)
	at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source)
	at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(Unknown Source)
	at com.infosys.finanztools.fms.test.data.service.TestClass.main(TestClass.java:23)
Caused by: java.io.EOFException: SSL peer shut down incorrectly
	at com.sun.net.ssl.internal.ssl.InputRecord.read(Unknown Source)
	... 9 more

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

 System.setProperty("java.protocol.handler.pkgs",
        "com.sun.net.ssl.internal.www.protocol");
   Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());

Comments

comments

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.

Comments

  1. 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 says:

      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?

      Thanks,
      Krishna

  2. Hi… i want to use this code, just a few clarifications. Suppose i have a file in http://zai.devserver.com:7778/hrwfapp/htdocs/practice/107405.docx. there is a hyper link where i need to give the link what should be given in this?

  3. Need to upload files to https server in android using php

Speak Your Mind

*

Close
Please support the site
By clicking any of these buttons you help our site to get better