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:
- Java Tutorials
- Java EE Tutorials
- Design Patterns Tutorials
- Java File IO Tutorials
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());