FTP客户端(利用sun net ftp FtpClient实现)

               

FTP客户端(利用sun.net.ftp.FtpClient实现)

    昨天帮同事弄java上传文件到ftp服务器,用的sun.net.ftp.FtpClient,感觉很简单,也很好用,写了个小例子,留作备份。

    这个小例子实现了文件(或文件夹)上传,文件下载,取得某目录下文件列表等功能,对于文件夹下载还没有实现,改天有空改完了再贴上来吧。由于通过设定好的用户名、密码登陆到ftp server时,连接到的目录不一定是根目录,有可能是根目录下很深层的一个子目录,比如“usr/why/test/ftp/upload/exercise/20090730”,所以我觉得针对当前连接目录进行操作,要比每次都从根目录开始指定方便。

Java代码   收藏代码
  1. package com.why.ftp;  
  2.   
  3. import java.io.DataInputStream;  
  4. import java.io.File;  
  5. import java.io.FileInputStream;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.OutputStream;  
  9. import java.util.ArrayList;  
  10. import java.util.List;  
  11. import java.util.StringTokenizer;  
  12. import sun.net.TelnetInputStream;  
  13. import sun.net.TelnetOutputStream;  
  14. import sun.net.ftp.FtpClient;  
  15.   
  16. /** 
  17.  * ftp上传,下载 
  18.  * @author why 2009-07-30 
  19.  * 
  20.  */  
  21. public class FtpUtil {  
  22.   
  23.     private String ip = "";  
  24.   
  25.     private String username = "";  
  26.   
  27.     private String password = "";  
  28.   
  29.     private int port = -1;  
  30.   
  31.     private String path = "";  
  32.   
  33.     FtpClient ftpClient = null;  
  34.   
  35.     OutputStream os = null;  
  36.   
  37.     FileInputStream is = null;  
  38.   
  39.     public FtpUtil(String serverIP, String username, String password) {  
  40.         this.ip = serverIP;  
  41.         this.username = username;  
  42.         this.password = password;  
  43.     }  
  44.       
  45.     public FtpUtil(String serverIP, int port, String username, String password) {  
  46.         this.ip = serverIP;  
  47.         this.username = username;  
  48.         this.password = password;  
  49.         this.port = port;  
  50.     }  
  51.   
  52.     /** 
  53.      * 连接ftp服务器 
  54.      *  
  55.      * @throws IOException 
  56.      */  
  57.     public boolean connectServer(){  
  58.         ftpClient = new FtpClient();  
  59.         try {  
  60.             if(this.port != -1){  
  61.                     ftpClient.openServer(this.ip,this.port);  
  62.             }else{  
  63.                 ftpClient.openServer(this.ip);  
  64.             }  
  65.             ftpClient.login(this.username, this.password);  
  66.             if (this.path.length() != 0){  
  67.                 ftpClient.cd(this.path);// path是ftp服务下主目录的子目录             
  68.             }  
  69.             ftpClient.binary();// 用2进制上传、下载  
  70.             System.out.println("已登录到\"" + ftpClient.pwd() + "\"目录");  
  71.             return true;  
  72.         }catch (IOException e){  
  73.             e.printStackTrace();  
  74.             return false;  
  75.         }  
  76.     }  
  77.       
  78.     /** 
  79.      * 断开与ftp服务器连接 
  80.      *  
  81.      * @throws IOException 
  82.      */  
  83.     public boolean closeServer(){  
  84.         try{  
  85.             if (is != null) {  
  86.                 is.close();  
  87.             }  
  88.             if (os != null) {  
  89.                 os.close();  
  90.             }  
  91.             if (ftpClient != null) {  
  92.                 ftpClient.closeServer();  
  93.             }  
  94.             System.out.println("已从服务器断开");  
  95.             return true;  
  96.         }catch(IOException e){  
  97.             e.printStackTrace();  
  98.             return false;  
  99.         }  
  100.     }  
  101.       
  102.     /** 
  103.      * 检查文件夹在当前目录下是否存在 
  104.      * @param dir 
  105.      * @return 
  106.      */  
  107.      private boolean isDirExist(String dir){  
  108.          String pwd = "";  
  109.          try {  
  110.              pwd = ftpClient.pwd();  
  111.              ftpClient.cd(dir);  
  112.              ftpClient.cd(pwd);  
  113.          }catch(Exception e){  
  114.              return false;  
  115.          }  
  116.          return true;   
  117.      }  
  118.       
  119.     /** 
  120.      * 在当前目录下创建文件夹 
  121.      * @param dir 
  122.      * @return 
  123.      * @throws Exception 
  124.      */  
  125.      private boolean createDir(String dir){  
  126.          try{  
  127.             ftpClient.ascii();  
  128.             StringTokenizer s = new StringTokenizer(dir, "/"); //sign  
  129.             s.countTokens();  
  130.             String pathName = ftpClient.pwd();  
  131.             while(s.hasMoreElements()){  
  132.                 pathName = pathName + "/" + (String) s.nextElement();  
  133.                 try {  
  134.                     ftpClient.sendServer("MKD " + pathName + "\r\n");  
  135.                 } catch (Exception e) {  
  136.                     e = null;  
  137.                     return false;  
  138.                 }  
  139.                 ftpClient.readServerResponse();  
  140.             }  
  141.             ftpClient.binary();  
  142.             return true;  
  143.         }catch (IOException e1){  
  144.             e1.printStackTrace();  
  145.             return false;  
  146.         }  
  147.      }  
  148.        
  149.      /** 
  150.       * ftp上传 
  151.       * 如果服务器段已存在名为filename的文件夹,该文件夹中与要上传的文件夹中同名的文件将被替换 
  152.       *  
  153.       * @param filename 要上传的文件(或文件夹)名 
  154.       * @return 
  155.       * @throws Exception 
  156.       */  
  157.     public boolean upload(String filename){  
  158.         String newname = "";  
  159.         if(filename.indexOf("/") > -1){  
  160.             newname = filename.substring(filename.lastIndexOf("/") + 1);  
  161.         }else{  
  162.             newname = filename;  
  163.         }  
  164.         return upload(filename, newname);  
  165.     }  
  166.        
  167.      /** 
  168.       * ftp上传 
  169.       * 如果服务器段已存在名为newName的文件夹,该文件夹中与要上传的文件夹中同名的文件将被替换 
  170.       *  
  171.       * @param fileName 要上传的文件(或文件夹)名 
  172.       * @param newName 服务器段要生成的文件(或文件夹)名 
  173.       * @return 
  174.       */  
  175.      public boolean upload(String fileName, String newName){  
  176.          try{  
  177.              String savefilename = new String(fileName.getBytes("ISO-8859-1"), "GBK");   
  178.              File file_in = new File(savefilename);//打开本地待长传的文件  
  179.              if(!file_in.exists()){  
  180.                  throw new Exception("此文件或文件夹[" + file_in.getName() + "]有误或不存在!");  
  181.              }  
  182.              if(file_in.isDirectory()){  
  183.                  upload(file_in.getPath(),newName,ftpClient.pwd());  
  184.              }else{  
  185.                  uploadFile(file_in.getPath(),newName);  
  186.              }  
  187.                
  188.              if(is != null){  
  189.                  is.close();  
  190.              }  
  191.              if(os != null){  
  192.                  os.close();  
  193.              }  
  194.              return true;  
  195.          }catch(Exception e){   
  196.                 e.printStackTrace();   
  197.                 System.err.println("Exception e in Ftp upload(): " + e.toString());   
  198.                 return false;  
  199.          }finally{  
  200.              try{  
  201.                  if(is != null){  
  202.                      is.close();  
  203.                  }  
  204.                  if(os != null){   
  205.                      os.close();   
  206.                  }  
  207.              }catch(IOException e){  
  208.                  e.printStackTrace();  
  209.             }   
  210.          }  
  211.      }  
  212.        
  213.      /** 
  214.       * 真正用于上传的方法 
  215.       * @param fileName 
  216.       * @param newName 
  217.       * @param path 
  218.       * @throws Exception 
  219.       */  
  220.      private void upload(String fileName, String newName,String path) throws Exception{  
  221.              String savefilename = new String(fileName.getBytes("ISO-8859-1"), "GBK");   

猜你喜欢

转载自blog.csdn.net/qq_44945073/article/details/89430910