kelson8

FTPClient.java

Jun 5th, 2014
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.35 KB | None | 0 0
  1. package me.Kelson;
  2. //This class may be used later
  3.  
  4. import java.io.BufferedInputStream;
  5. import java.io.BufferedOutputStream;
  6. import java.io.FileInputStream;
  7. import java.io.FileOutputStream;
  8. import java.io.InputStream;
  9. import java.io.OutputStream;
  10. import java.io.PrintWriter;
  11. import java.io.StringWriter;
  12. import java.net.URL;
  13. import java.net.URLConnection;
  14. public class FTPClient {
  15.    
  16.     private URLConnection client;
  17.     private String host;
  18.     private String username;
  19.     private String password;
  20.     private String remoteFile;
  21.     private String error;
  22.     private String success;
  23.  
  24.     public FTPClient(String host, String username, String password){
  25.         this.host = host;
  26.         this.username = username;
  27.         this.password = password;
  28.     }
  29.     public FTPClient(){
  30.        
  31.     }
  32.    
  33.     public void setHost(String host){
  34.         this.host = host;
  35.     }
  36.    
  37.     public void setUser(String username){
  38.         this.username = username;
  39.     }
  40.    
  41.     public void setPassword(String password){
  42.         this.password = password;
  43.     }
  44.    
  45.     public void setRemoteFile(String file){
  46.         remoteFile = file;
  47.     }
  48.    
  49.     public synchronized String getSucMsg(){
  50.         if(success != null)
  51.             return success;
  52.         return null;
  53.     }
  54.    
  55.     public synchronized String getErrMsg(){
  56.         if(error != null)
  57.             return error;
  58.         return null;
  59.      }
  60.    
  61.     public synchronized boolean uploadFile(String fileLoc){
  62.         try {
  63.             InputStream is = new FileInputStream(fileLoc);
  64.             @SuppressWarnings("resource")
  65.             BufferedInputStream bis = new BufferedInputStream(is);
  66.             OutputStream os = client.getOutputStream();
  67.             BufferedOutputStream bos = new BufferedOutputStream(os);
  68.            
  69.             byte[] buffer = new byte[1024];
  70.            
  71.             int count;
  72.             while((count = bis.read(buffer)) > 0)
  73.                 bos.write(buffer, 0, count);
  74.             bos.close();
  75.             success="Uploaded!";
  76.             return true;
  77.         } catch(Exception e){
  78.             StringWriter sw = new StringWriter();
  79.             PrintWriter pw = new PrintWriter(sw, true);
  80.             e.printStackTrace(pw);
  81.             error = sw.getBuffer().toString();
  82.             return false;
  83.         }
  84.     }
  85.    
  86.     public synchronized boolean downloadFile(String fileLoc){
  87.         try {
  88.         InputStream is = client.getInputStream();
  89.         BufferedInputStream bis = new BufferedInputStream(is);
  90.         OutputStream os = new FileOutputStream(fileLoc);
  91.         BufferedOutputStream bos = new BufferedOutputStream(os);
  92.        
  93.         byte[] buffer = new byte[1024];
  94.        
  95.         int count;
  96.         while ((count = bis.read(buffer)) > 0)
  97.             bos.write(buffer, 0, count);
  98.         bos.close();
  99.         is.close();
  100.         success="Downloaded!";
  101.        
  102.         return true;
  103.         } catch(Exception e){
  104.             StringWriter sw = new StringWriter();
  105.             PrintWriter pw = new PrintWriter(sw, true);
  106.             e.printStackTrace(pw);
  107.             error = sw.getBuffer().toString();
  108.             return false;
  109.         }
  110.     }
  111.     public synchronized boolean connect(){
  112.         try {
  113.             URL url = new URL("ftp://" + username + ":" + password + "@"+ host
  114.                     + "/" + remoteFile + ";type=i");
  115.             client = url.openConnection();
  116.             return true;
  117.         } catch(Exception e){
  118.             StringWriter sw = new StringWriter();
  119.             PrintWriter pw = new PrintWriter(sw, true);
  120.             e.printStackTrace(pw);
  121.             error = sw.getBuffer().toString();
  122.             return false;
  123.         }
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment