Advertisement
Guest User

Untitled

a guest
Feb 24th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.94 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.     public static URLConnection getConnection(String filePath)
  67.     {
  68.         return getConnection(filePath, "i");
  69.     }
  70.    
  71.     public static URLConnection getConnection(String filePath, String mode)
  72.     {
  73.         try {
  74.             URL url = new URL("ftp://" + URLEncoder.encode(user, "UTF-8") + ":" + URLEncoder.encode(pass, "UTF-8") + "@" + host + "/" + URLEncoder.encode(filePath, "UTF-8") + ";type=" + mode);
  75.            
  76.             URLConnection conn = url.openConnection();
  77.            
  78.             return conn;
  79.            
  80.         } catch (IOException ex) {
  81.             ex.printStackTrace();
  82.            
  83.             return null;
  84.         }
  85.     }
  86.    
  87.     /*
  88.      * This method checks the connection to the specified file.
  89.      *
  90.      * It tests that it can both be read from but not written to, as
  91.      * it will create the file and there is no way to delete it.
  92.      *
  93.      * @param filePath - The file that you want to check the connection to
  94.      * @returns boolean - true if it can connect to the file, false if not.
  95.      */
  96.     public static boolean testConnectionToFile(String filePath)
  97.     {        
  98.         try
  99.         {  
  100.             URLConnection conn = getConnection(filePath);
  101.             InputStream inputStream = conn.getInputStream();
  102.             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
  103.  
  104.             inputStream.close();
  105.            
  106.             return true;
  107.            
  108.         } catch (FileNotFoundException e)
  109.         {
  110.             System.out.println("Could not find file " + filePath + " on FTP server!");
  111.  
  112.             return false;
  113.        
  114.         } catch (IOException ex)
  115.         {
  116.             ex.printStackTrace();
  117.            
  118.             return false;
  119.         }
  120.     }
  121.    
  122.     /*
  123.      * This method gets all the strings that are in a file and
  124.      * returns them in a list. For the developers manipulation.
  125.      *
  126.      * @param filePath - The path to the file they wish to download
  127.      *
  128.      * @returns List<String> - returns the list of all strings in the file.
  129.      *                         can return null if the file doesn't exist, or
  130.      *                         there is another error.
  131.      */
  132.     public static List<String> getFileStrings(String filePath)
  133.     {
  134.         List<String> results = new ArrayList<String>();
  135.        
  136.         try
  137.         {  
  138.             URLConnection conn = getConnection(filePath);
  139.             InputStream inputStream = conn.getInputStream();
  140.             BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
  141.            
  142.             String line = null;
  143.  
  144.             while ((line = reader.readLine()) != null) {
  145.                 results.add(line);
  146.             }
  147.  
  148.             inputStream.close();
  149.            
  150.         } catch (FileNotFoundException e)
  151.         {
  152.             System.out.println("Could not find file " + filePath + " on FTP server!");
  153.            
  154.             return null;
  155.         }
  156.         catch (IOException ex)
  157.         {
  158.             ex.printStackTrace();
  159.            
  160.             return null;
  161.         }
  162.        
  163.         return results;
  164.     }
  165.    
  166.     /*
  167.      * This method writes a list of strings to the specified file.
  168.      * It also CLEARS the existing file so make sure to first FETCH
  169.      * everything from the file if you wish to append it.
  170.      *
  171.      * @param filePath - The file location where we are uploading to
  172.      * @param strings - The list of strings that are to be written to the file
  173.      *                  each string will be seperated by a new line.
  174.      */
  175.     public static void writeAllStrings(String filePath, List<String> strings)
  176.     {
  177.         try
  178.         {
  179.             URLConnection conn = getConnection(filePath);
  180.            
  181.             OutputStream outputStream = conn.getOutputStream();
  182.             BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
  183.            
  184.             for (String s : strings)
  185.             {
  186.                 writer.write(s + "\n");
  187.             }
  188.            
  189.             writer.close();
  190.             outputStream.close();
  191.            
  192.         } catch (IOException e)
  193.         {
  194.             e.printStackTrace();
  195.         }
  196.     }
  197.    
  198.     /*
  199.      * This method provides the ability to append more strings to the end of a file
  200.      *
  201.      * @param filePath - The path of the file on the FTP server to be appended
  202.      * @param strings - The list of strings that are to be appended on the file.
  203.      *                  Each string will be seperated by a new line.
  204.      *
  205.      */
  206.     public static void appendToFile(String filePath, List<String> strings)
  207.     {
  208.         List<String> current = getFileStrings(filePath);
  209.        
  210.         if (current == null)
  211.         {
  212.             current = strings;
  213.         }
  214.         else
  215.         {
  216.             current.addAll(strings);
  217.         }
  218.        
  219.         writeAllStrings(filePath, current);
  220.     }
  221.    
  222.     /*
  223.      * This method downloads text from a file on the FTP server and saves it locally.
  224.      *
  225.      * @param fromFile - The path of the file to be downloaded
  226.      * @param toFile - The path of the file to be outputted to
  227.      */
  228.     public static void downloadTextToLocalFile(String fromFile, String toFile)
  229.     {
  230.         List<String> fileData = getFileStrings(fromFile);
  231.        
  232.         try
  233.         {
  234.             PrintWriter out = new PrintWriter(toFile);
  235.            
  236.             for (String line : fileData)
  237.             {
  238.                 out.println(line);
  239.             }
  240.            
  241.             out.close();
  242.            
  243.             System.out.println("Successfully downloaded file");
  244.            
  245.         } catch (FileNotFoundException e)
  246.         {
  247.             System.out.println("Unable to find the file " + toFile);
  248.         }
  249.     }
  250.    
  251.     /*
  252.      * This method downloads the file in bytes from the FTP server.
  253.      *
  254.      * @param fromFile - The path of the file to download on the FTP server
  255.      * @param toFile - The path of the file to store the file from the FTP
  256.      *
  257.      */
  258.     public static void downloadBytesToLocalFile(String fromFile, String toFile)
  259.     {
  260.         try
  261.         {
  262.             URLConnection conn = getConnection(fromFile);
  263.            
  264.             InputStream inputSteam = conn.getInputStream();
  265.             BufferedInputStream bis = new BufferedInputStream(inputSteam);
  266.            
  267.             OutputStream outputStream = new FileOutputStream(toFile);
  268.             BufferedOutputStream bos = new BufferedOutputStream(outputStream);
  269.            
  270.             byte[] buffer = new byte[1024];
  271.            
  272.             int count;
  273.            
  274.             while ((count = bis.read(buffer)) > 0)
  275.             {
  276.                 bos.write(buffer, 0, count);
  277.             }
  278.            
  279.             bos.close();
  280.             inputSteam.close();
  281.            
  282.             System.out.println("Successfully downloaded File");
  283.            
  284.            
  285.         } catch (FileNotFoundException e)
  286.         {
  287.             System.out.println("Unable to find the file " + toFile);
  288.         }
  289.         catch(Exception e)
  290.         {
  291.             e.printStackTrace();
  292.         }
  293.     }
  294. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement