skip to main | skip to sidebar

Java Programs and Examples with Output

Pages

▼
 
  • RSS
  • Twitter
Wednesday, September 19, 2012

File Transfer via Secured Connection

Posted by Admin at 9:56 AM – 0 comments
 

"File Transfer via Secured Connection" provides a sample code framework that implements secure communication between a Java client and an HTTPS (Hyper Text Transfer Protocol Secure) server. Almost any Web server available today provides a mechanism for requesting data, using HTTPS. From the client perspective, the simplicity of the S at the end of the familiar HTTP is deceiving. The browser is actually doing a considerable amount of behind-the-scenes work to ensure that no one has tampered with or monitored the information that you requested.


import java.io.*;
import java.net.*;
import java.security.*;
import java.security.Security;
import com.sun.net.ssl.internal.ssl.Provider;    //This package needs to be imported to enable SSL communication

public class SSL_URL_Connect
{

 public SSL_URL_Connect()
 {
 }

 public static void main(String args[])
 {
  java.security.Security.addProvider(new sun.security.provider.Sun());
  java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  System.setProperty("java.protocol.handler.pkgs", "com.sun.net.ssl.internal.www.protocol");

  try
  {
   java.util.Properties properties = System.getProperties();
   System.setProperty("proxySet", "true");
   System.setProperty("https.proxyHost", "<qualified name of the Web Server Host>");  // -- Web Server host Parameter to be substituted
   System.setProperty("https.proxyPort", "<SSL port>");    // -- SSL port Parameter to be substituted
   
   URL url = new URL("https://<Secured Web URL>");

   class TestAuthenticator extends Authenticator
   {
    private String username="<Authenticating username>";   // -- Username Parameter to be substituted
    private String password="<Authenticating password>";   // -- Password Parameter to be substituted

    public PasswordAuthentication getPasswordAuthentication()
    {
     return new PasswordAuthentication(username,password.toCharArray());
    }
   }

   Authenticator.setDefault(new TestAuthenticator());
   System.out.println("Authenticator set .... ");

   URLConnection urlconnection = url.openConnection();
   System.out.println("Connected to the site");

   BufferedReader bufferedreader = new BufferedReader(new InputStreamReader(urlconnection.getInputStream()));
   String strline = "";

   String download_file="<File to be downloaded>";    // -- Dwonloaded FileName Parameter to be substituted

   File file = new File(download_file);

   if(file.exists() && file.delete())
   {
    System.out.println("Existing File is deleted");
   }

   FileWriter filewriter = new FileWriter(download_file);

   while((strline = bufferedreader.readLine()) != null)
   {
    filewriter.write(strline + "rn");
   }
   
   filewriter.close();
  }
  catch(Exception exception)
  {
   System.out.println("e " + exception);
  }
 }
}


Leave a Reply

Newer Post Older Post
Subscribe to: Post Comments ( Atom )
  • Popular
  • Recent
  • Archives
Powered by Blogger.
 
 
 
© 2011 Java Programs and Examples with Output | Designs by Web2feel & Fab Themes

Bloggerized by DheTemplate.com - Main Blogger