Advertisement
Guest User

Untitled

a guest
May 25th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1. package network;
  2.  
  3.  import java.io.*;
  4.  import java.net.*;
  5.  
  6.  /**
  7.   * @author B_S
  8.   *
  9.   */
  10.  public class NetworkIO {
  11.  
  12.     /**
  13.          * Downloads {@code source} file to {@code destination} location.
  14.          *
  15.      * @param source URL to the source file.
  16.          * @param destination path to the destination file.
  17.      */
  18.     public static void getFile(String source, String destination) throws Exception {
  19.         final String tiUser = "hereComesYourUsername";
  20.         final char[] tiPassword = "hereComesYourPassword".toCharArray();
  21.        
  22.         Authenticator.setDefault(new Authenticator(){
  23.             protected PasswordAuthentication getPasswordAuthentication() {
  24.                 return new PasswordAuthentication(tiUser, tiPassword);
  25.             }
  26.         });
  27.  
  28.         DataInputStream input = null;
  29.         FileOutputStream fout = null;
  30.         DataOutputStream output = null;
  31.         try {
  32.             URL newURL = new URL(source);
  33.             URLConnection fileStream = newURL.openConnection();
  34.            
  35.             input = new DataInputStream(fileStream.getInputStream());
  36.             fout = new FileOutputStream(destination);
  37.             output = new DataOutputStream(fout);
  38.            
  39.             int data;
  40.            
  41.             while ((data = input.read()) != -1) {
  42.                 fout.write(data);
  43.             }
  44.             System.out.println("Download is complete." );
  45.         } catch (Exception e){
  46.              e.printStackTrace();
  47.           } finally {
  48.              try{
  49.                input.close();
  50.                fout.flush();
  51.                output.close();      
  52.              } catch(Exception e){e.printStackTrace();}
  53.              
  54.             }
  55.  
  56.        
  57.        
  58.        
  59.     }
  60.  
  61.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement