Advertisement
Guest User

Untitled

a guest
Feb 24th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.88 KB | None | 0 0
  1. /*
  2.  * FTPConnector
  3.  *
  4.  * This class is used to connect to and interact with an
  5.  * FTP server. The methods allow you to download and upload
  6.  * files to and from the server.
  7.  *
  8.  * @author - Joseph Shufflebotham
  9.  *
  10.  * © Copyright 2016
  11.  */
  12. package me.JoeShuff.Test;
  13.  
  14. import java.io.BufferedInputStream;
  15. import java.io.BufferedOutputStream;
  16. import java.io.BufferedReader;
  17. import java.io.BufferedWriter;
  18. import java.io.FileNotFoundException;
  19. import java.io.FileOutputStream;
  20. import java.io.IOException;
  21. import java.io.InputStream;
  22. import java.io.InputStreamReader;
  23. import java.io.OutputStream;
  24. import java.io.OutputStreamWriter;
  25. import java.io.PrintWriter;
  26. import java.net.URL;
  27. import java.net.URLConnection;
  28. import java.net.URLEncoder;
  29. import java.util.ArrayList;
  30. import java.util.List;
  31.  
  32. public class FTPClient {
  33.    
  34.     private String host = null;
  35.     private String user = null;
  36.     private String pass = null;
  37.    
  38.     /*
  39.      * This method sets the default  values for the server informations
  40.      *
  41.      * @param host - The host IP or url as a String
  42.      * @param pass - The password for the FTP server
  43.      * @param username - The username for the FTP server
  44.      */
  45.     public FTPClient(String host, String username, String pass)
  46.     {
  47.         this.host = host;
  48.         this.user = username;
  49.         this.pass = pass;
  50.     }
  51.    
  52.     /*
  53.      * This method sets the default  values for the server informations
  54.      *
  55.      * @param host - The host IP or url as a String
  56.      * @param pass - The password for the FTP server
  57.      * @param username - The username for the FTP server
  58.      */
  59.     public void changeServerInfo(String host, String username, String pass)
  60.     {
  61.         this.host = host;
  62.         this.user = username;
  63.         this.pass = pass;
  64.     }
  65.    
  66.     /*
  67.      * This method gets the URL connection to the specified file with default mode
  68.      *
  69.      * @param filePath - The path of the file on the server to be found
  70.      */
  71.     public URLConnection getConnection(String filePath)
  72.     {
  73.         return getConnection(filePath, "i");
  74.     }
  75.    
  76.     /*
  77.      * This method gets the URL connection to the specified file with default mode
  78.      *
  79.      * @param filePath - The path of the file on the server to be found
  80.      * @param mode - The mode of which the FTP request needs to be sent in.
  81.      */
  82.     public URLConnection getConnection(String filePath, String mode)
  83.     {
  84.         try {
  85.             URL url = new URL("ftp://" + URLEncoder.encode(user, "UTF-8") + ":" + URLEncoder.encode(pass, "UTF-8") + "@" + host + "/" + URLEncoder.encode(filePath, "UTF-8") + ";type=" + mode);
  86.            
  87.             URLConnection conn = url.openConnection();
  88.            
  89.             return conn;
  90.            
  91.         } catch (IOException ex) {
  92.             ex.printStackTrace();
  93.            
  94.             return null;
  95.         }
  96.     }
  97.    
  98.     /*
  99.      * This method checks the connection to the specified file.
  100.      *
  101.      * It tests that it can both be read from but not written to, as
  102.      * it will create the file and there is no way to delete it.
  103.      *
  104.      * @param filePath - The file that you want to check the connection to
  105.      * @returns boolean - true if it can connect to the file, false if not.
  106.      */
  107.     public boolean testConnectionToFile(String filePath)
  108.     {        
  109.         try
  110.         {  
  111.             URLConnection conn = getConnection(filePath);
  112.             InputStream inputStream = conn.getInputStream();
  113.             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
  114.  
  115.             inputStream.close();
  116.            
  117.             return true;
  118.            
  119.         } catch (FileNotFoundException e)
  120.         {
  121.             System.out.println("Could not find file " + filePath + " on FTP server!");
  122.  
  123.             return false;
  124.        
  125.         } catch (NullPointerException e)
  126.         {
  127.             System.out.println("Unable to connect to the FTP server");
  128.             return false;
  129.         }
  130.         catch (IOException ex)
  131.         {
  132.             ex.printStackTrace();
  133.            
  134.             return false;
  135.         }
  136.     }
  137.    
  138.     /*
  139.      * This method gets all the strings that are in a file and
  140.      * returns them in a list. For the developers manipulation.
  141.      *
  142.      * @param filePath - The path to the file they wish to download
  143.      *
  144.      * @returns List<String> - returns the list of all strings in the file.
  145.      *                         can return null if the file doesn't exist, or
  146.      *                         there is another error.
  147.      */
  148.     public List<String> getFileStrings(String filePath)
  149.     {
  150.         List<String> results = new ArrayList<String>();
  151.        
  152.         try
  153.         {  
  154.             URLConnection conn = getConnection(filePath);
  155.             InputStream inputStream = conn.getInputStream();
  156.             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
  157.            
  158.             String line = null;
  159.  
  160.             while ((line = reader.readLine()) != null) {
  161.                 results.add(line);
  162.             }
  163.  
  164.             inputStream.close();
  165.            
  166.         } catch (FileNotFoundException e)
  167.         {
  168.             System.out.println("Could not find file " + filePath + " on FTP server!");
  169.            
  170.             return null;
  171.         }
  172.         catch (NullPointerException e)
  173.         {
  174.             System.out.println("Unable to connect to the FTP server");
  175.            
  176.             return null;
  177.            
  178.         } catch (IOException ex)
  179.         {
  180.             ex.printStackTrace();
  181.            
  182.             return null;
  183.         }
  184.        
  185.         return results;
  186.     }
  187.    
  188.     /*
  189.      * This method writes a list of strings to the specified file.
  190.      * It also CLEARS the existing file so make sure to first FETCH
  191.      * everything from the file if you wish to append it.
  192.      *
  193.      * @param filePath - The file location where we are uploading to
  194.      * @param strings - The list of strings that are to be written to the file
  195.      *                  each string will be seperated by a new line.
  196.      */
  197.     public void writeAllStrings(String filePath, List<String> strings)
  198.     {
  199.         try
  200.         {
  201.             URLConnection conn = getConnection(filePath);
  202.            
  203.             OutputStream outputStream = conn.getOutputStream();
  204.             BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
  205.            
  206.             for (String s : strings)
  207.             {
  208.                 writer.write(s + "\n");
  209.             }
  210.            
  211.             writer.close();
  212.             outputStream.close();
  213.            
  214.         } catch (NullPointerException e)
  215.         {
  216.             System.out.println("Unable to connect to the FTP server");
  217.         }
  218.         catch (IOException e)
  219.         {
  220.             e.printStackTrace();
  221.         }
  222.     }
  223.    
  224.     /*
  225.      * This method provides the ability to append more strings to the end of a file
  226.      *
  227.      * @param filePath - The path of the file on the FTP server to be appended
  228.      * @param strings - The list of strings that are to be appended on the file.
  229.      *                  Each string will be seperated by a new line.
  230.      *
  231.      */
  232.     public void appendToFile(String filePath, List<String> strings)
  233.     {
  234.         List<String> current = getFileStrings(filePath);
  235.        
  236.         if (current == null)
  237.         {
  238.             current = strings;
  239.         }
  240.         else
  241.         {
  242.             current.addAll(strings);
  243.         }
  244.        
  245.         writeAllStrings(filePath, current);
  246.     }
  247.    
  248.     /*
  249.      * This method downloads text from a file on the FTP server and saves it locally.
  250.      *
  251.      * @param fromFile - The path of the file to be downloaded
  252.      * @param toFile - The path of the file to be outputted to
  253.      */
  254.     public void downloadTextToLocalFile(String fromFile, String toFile)
  255.     {
  256.         List<String> fileData = getFileStrings(fromFile);
  257.        
  258.         try
  259.         {
  260.             PrintWriter out = new PrintWriter(toFile);
  261.            
  262.             for (String line : fileData)
  263.             {
  264.                 out.println(line);
  265.             }
  266.            
  267.             out.close();
  268.            
  269.             System.out.println("Successfully downloaded file");
  270.            
  271.         } catch (NullPointerException e)
  272.         {
  273.             System.out.println("Unable to connect to the FTP server");
  274.         }
  275.         catch (FileNotFoundException e)
  276.         {
  277.             System.out.println("Unable to find the file " + toFile);
  278.         }
  279.     }
  280.    
  281.     /*
  282.      * This method downloads the file in bytes from the FTP server.
  283.      *
  284.      * @param fromFile - The path of the file to download on the FTP server
  285.      * @param toFile - The path of the file to store the file from the FTP
  286.      *
  287.      */
  288.     public void downloadBytesToLocalFile(String fromFile, String toFile)
  289.     {
  290.         try
  291.         {
  292.             URLConnection conn = getConnection(fromFile);
  293.            
  294.             InputStream inputSteam = conn.getInputStream();
  295.             BufferedInputStream bis = new BufferedInputStream(inputSteam);
  296.            
  297.             OutputStream outputStream = new FileOutputStream(toFile);
  298.             BufferedOutputStream bos = new BufferedOutputStream(outputStream);
  299.            
  300.             byte[] buffer = new byte[1024];
  301.            
  302.             int count;
  303.            
  304.             while ((count = bis.read(buffer)) > 0)
  305.             {
  306.                 bos.write(buffer, 0, count);
  307.             }
  308.            
  309.             bos.close();
  310.             inputSteam.close();
  311.            
  312.             System.out.println("Successfully downloaded File");
  313.            
  314.            
  315.         } catch (FileNotFoundException e)
  316.         {
  317.             System.out.println("Unable to find the file " + toFile);
  318.         }
  319.         catch (NullPointerException e)
  320.         {
  321.             System.out.println("Unable to connect to the FTP server");
  322.         }
  323.         catch(Exception e)
  324.         {
  325.             e.printStackTrace();
  326.         }
  327.     }
  328. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement