Advertisement
Guest User

Untitled

a guest
Feb 24th, 2016
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.34 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.SkyBasers.FTP;
  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 static String host = null;
  35.     private static String user = null;
  36.     private static String pass = null;
  37.    
  38.     /*
  39.      * This method sets the default static 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 static void bootup(String host, String username, String pass)
  46.     {
  47.         FTPClient.host = host;
  48.         FTPClient.user = username;
  49.         FTPClient.pass = pass;
  50.     }
  51.    
  52.     /*
  53.      * This method sets the default static 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 static void changeServerInfo(String host, String username, String pass)
  60.     {
  61.         FTPClient.host = host;
  62.         FTPClient.user = username;
  63.         FTPClient.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 static 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 static 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 static 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 (IOException ex)
  126.         {
  127.             ex.printStackTrace();
  128.            
  129.             return false;
  130.         }
  131.     }
  132.    
  133.     /*
  134.      * This method gets all the strings that are in a file and
  135.      * returns them in a list. For the developers manipulation.
  136.      *
  137.      * @param filePath - The path to the file they wish to download
  138.      *
  139.      * @returns List<String> - returns the list of all strings in the file.
  140.      *                         can return null if the file doesn't exist, or
  141.      *                         there is another error.
  142.      */
  143.     public static List<String> getFileStrings(String filePath)
  144.     {
  145.         List<String> results = new ArrayList<String>();
  146.        
  147.         try
  148.         {  
  149.             URLConnection conn = getConnection(filePath);
  150.             InputStream inputStream = conn.getInputStream();
  151.             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
  152.            
  153.             String line = null;
  154.  
  155.             while ((line = reader.readLine()) != null) {
  156.                 results.add(line);
  157.             }
  158.  
  159.             inputStream.close();
  160.            
  161.         } catch (FileNotFoundException e)
  162.         {
  163.             System.out.println("Could not find file " + filePath + " on FTP server!");
  164.            
  165.             return null;
  166.         }
  167.         catch (IOException ex)
  168.         {
  169.             ex.printStackTrace();
  170.            
  171.             return null;
  172.         }
  173.        
  174.         return results;
  175.     }
  176.    
  177.     /*
  178.      * This method writes a list of strings to the specified file.
  179.      * It also CLEARS the existing file so make sure to first FETCH
  180.      * everything from the file if you wish to append it.
  181.      *
  182.      * @param filePath - The file location where we are uploading to
  183.      * @param strings - The list of strings that are to be written to the file
  184.      *                  each string will be seperated by a new line.
  185.      */
  186.     public static void writeAllStrings(String filePath, List<String> strings)
  187.     {
  188.         try
  189.         {
  190.             URLConnection conn = getConnection(filePath);
  191.            
  192.             OutputStream outputStream = conn.getOutputStream();
  193.             BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
  194.            
  195.             for (String s : strings)
  196.             {
  197.                 writer.write(s + "\n");
  198.             }
  199.            
  200.             writer.close();
  201.             outputStream.close();
  202.            
  203.         } catch (IOException e)
  204.         {
  205.             e.printStackTrace();
  206.         }
  207.     }
  208.    
  209.     /*
  210.      * This method provides the ability to append more strings to the end of a file
  211.      *
  212.      * @param filePath - The path of the file on the FTP server to be appended
  213.      * @param strings - The list of strings that are to be appended on the file.
  214.      *                  Each string will be seperated by a new line.
  215.      *
  216.      */
  217.     public static void appendToFile(String filePath, List<String> strings)
  218.     {
  219.         List<String> current = getFileStrings(filePath);
  220.        
  221.         if (current == null)
  222.         {
  223.             current = strings;
  224.         }
  225.         else
  226.         {
  227.             current.addAll(strings);
  228.         }
  229.        
  230.         writeAllStrings(filePath, current);
  231.     }
  232.    
  233.     /*
  234.      * This method downloads text from a file on the FTP server and saves it locally.
  235.      *
  236.      * @param fromFile - The path of the file to be downloaded
  237.      * @param toFile - The path of the file to be outputted to
  238.      */
  239.     public static void downloadTextToLocalFile(String fromFile, String toFile)
  240.     {
  241.         List<String> fileData = getFileStrings(fromFile);
  242.        
  243.         try
  244.         {
  245.             PrintWriter out = new PrintWriter(toFile);
  246.            
  247.             for (String line : fileData)
  248.             {
  249.                 out.println(line);
  250.             }
  251.            
  252.             out.close();
  253.            
  254.             System.out.println("Successfully downloaded file");
  255.            
  256.         } catch (FileNotFoundException e)
  257.         {
  258.             System.out.println("Unable to find the file " + toFile);
  259.         }
  260.     }
  261.    
  262.     /*
  263.      * This method downloads the file in bytes from the FTP server.
  264.      *
  265.      * @param fromFile - The path of the file to download on the FTP server
  266.      * @param toFile - The path of the file to store the file from the FTP
  267.      *
  268.      */
  269.     public static void downloadBytesToLocalFile(String fromFile, String toFile)
  270.     {
  271.         try
  272.         {
  273.             URLConnection conn = getConnection(fromFile);
  274.            
  275.             InputStream inputSteam = conn.getInputStream();
  276.             BufferedInputStream bis = new BufferedInputStream(inputSteam);
  277.            
  278.             OutputStream outputStream = new FileOutputStream(toFile);
  279.             BufferedOutputStream bos = new BufferedOutputStream(outputStream);
  280.            
  281.             byte[] buffer = new byte[1024];
  282.            
  283.             int count;
  284.            
  285.             while ((count = bis.read(buffer)) > 0)
  286.             {
  287.                 bos.write(buffer, 0, count);
  288.             }
  289.            
  290.             bos.close();
  291.             inputSteam.close();
  292.            
  293.             System.out.println("Successfully downloaded File");
  294.            
  295.            
  296.         } catch (FileNotFoundException e)
  297.         {
  298.             System.out.println("Unable to find the file " + toFile);
  299.         }
  300.         catch(Exception e)
  301.         {
  302.             e.printStackTrace();
  303.         }
  304.     }
  305. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement