Java进行FTP操作


Java进行FTP操作,采用的是Apache的FTPClient操作,需要引入commons-net包

  1. import java.io.ByteArrayInputStream;  
  2. import java.io.FileOutputStream;  
  3. import java.io.IOException;  
  4. import java.net.SocketException;  
  5. import java.text.SimpleDateFormat;  
  6. import java.util.Date;  
  7.   
  8. import org.apache.commons.io.IOUtils;  
  9. import org.apache.commons.net.ftp.FTPClient;  
  10.   
  11. /** 
  12.  * class name:FTPFileTransmit <BR> 
  13.  * class description: please write your description <BR> 
  14.  * Remark: <BR> 
  15.  * @version 1.00 2011-8-9 
  16.  */  
  17. public class FTPFileTransmit {  
  18.   
  19.     private String ftpPath;  
  20.     private String ftpName;  
  21.     private String ftpPassword;  
  22.     private String ftpServerIP;  
  23.       
  24.     public FTPFileTransmit() {  
  25.         this.ftpPath = "xxx/xxx/";  
  26.         this.ftpName = "name";  
  27.         this.ftpPassword = "pass";  
  28.         this.ftpServerIP = "192.168.0.xx";  
  29.     }  
  30.       
  31.       
  32.     /** 
  33.      * Method name: saveInFTP <BR> 
  34.      * Description: 把文件存储在FTP上 <BR> 
  35.      * Remark: <BR> 
  36.      * @param FolderName            示例"xxx/xxx/" 
  37.      * @param FileName              示例"thefilename" 
  38.      * @param data                  byte[]数组 
  39.      * @return  boolean<BR> 
  40.      */  
  41.     public boolean saveInFTP (String FolderName, String FileName, byte[] data) {  
  42.         boolean flag = false;  
  43.           
  44.         // 创建FTP客户端   
  45.         FTPClient ftpClient = new FTPClient();  
  46.         // 输入流用于读取文件   
  47. //      FileInputStream fis = null;   
  48.         ByteArrayInputStream bis = null;  
  49.           
  50.         try {  
  51.             // 如果FolderName 和 FileName都不符合基本要求, 那么就没有必要进行ftp操作   
  52.             if (FolderName != null  
  53.                     && FolderName.compareTo("") != 0  
  54.                     && FileName != null  
  55.                     && FileName.compareTo("") != 0) {  
  56.   
  57.                 // 建立FTP连接   
  58.                 ftpClient.connect(this.ftpServerIP);  
  59.                   
  60.                 // 如果登录成功后, 才进行创建输入流   
  61.                 if (ftpClient.login(this.ftpName, this.ftpPassword)) {  
  62. //                  File srcClientFile = new File("C:/ParseXML.xml");   
  63.                       
  64.                     // 实例化输入流   
  65. //                  fis = new FileInputStream(srcClientFile);   
  66.                       
  67.                     if (ftpClient.changeWorkingDirectory(FolderName)) {  
  68.                         // 将byte[]写入到输入流中, 实例化   
  69.                         bis = new ByteArrayInputStream(data);  
  70.                           
  71.                         // 设置缓冲   
  72.                         ftpClient.setBufferSize(1024);  
  73.                           
  74.                         // 设置文件类型(二进制类型)   
  75.                         if (ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE)) {  
  76.                             flag = ftpClient.storeFile(FileName, bis);  
  77.                         }  
  78.                     }  
  79.                 }  
  80.             }  
  81.         } catch (SocketException e) {  
  82.             e.printStackTrace();  
  83.             flag = false;  
  84.         } catch (IOException e) {  
  85.             e.printStackTrace();  
  86.             flag = false;  
  87.         } catch (Exception e) {  
  88.             e.printStackTrace();  
  89.             flag = false;  
  90.         } finally {  
  91.             try {  
  92.                 // 关闭输入流   
  93.                 IOUtils.closeQuietly(bis);  
  94.                 // 关闭连接   
  95.                 ftpClient.disconnect();  
  96.             } catch (IOException e) {  
  97.                 e.printStackTrace();  
  98.             }  
  99.         }  
  100.           
  101.         return flag;  
  102.     }  
  103.       
  104.     /** 
  105.      * Method name: getFromFTP <BR> 
  106.      * Description: 从FTP上读取文件 <BR> 
  107.      * Remark: <BR> 
  108.      * @return  boolean<BR> 
  109.      */  
  110.     public boolean getFromFTP () {  
  111.         boolean flag = false;  
  112.           
  113.         // 创建FTP客户端   
  114.         FTPClient ftpClient = new FTPClient();  
  115.         // 输出流用于输出文件   
  116.         FileOutputStream fos = null;  
  117.           
  118.         try {  
  119.             // 建立FTP连接   
  120.             ftpClient.connect(this.ftpServerIP);  
  121.             // 如果登录成功后, 才进行创建输出流   
  122.             if (ftpClient.login(this.ftpName, this.ftpPassword)) {  
  123.                 // FTP文件   
  124.                 String distinationFile = "/name/xxx/xxx/xxx文件";  
  125.                 // 实例化输出流   
  126.                 fos = new FileOutputStream("C:/ParseXML_InFTP.xml");  
  127.                   
  128.                 // 设置缓冲   
  129.                 ftpClient.setBufferSize(1024);  
  130.                   
  131.                 // 设置文件类型(二进制类型)   
  132.                 if (ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE)) {  
  133.                     ftpClient.retrieveFile(distinationFile, fos);  
  134.                     flag = true;  
  135.                 }  
  136.             }  
  137.         } catch (SocketException e) {  
  138.             e.printStackTrace();  
  139.             flag = false;  
  140.         } catch (IOException e) {  
  141.             e.printStackTrace();  
  142.             flag = false;  
  143.         } catch (Exception e) {  
  144.             e.printStackTrace();  
  145.             flag = false;  
  146.         } finally {  
  147.             try {  
  148.                 // 关闭输出流   
  149.                 IOUtils.closeQuietly(fos);  
  150.                 // 关闭连接   
  151.                 ftpClient.disconnect();  
  152.             } catch (IOException e) {  
  153.                 e.printStackTrace();  
  154.             }  
  155.         }  
  156.           
  157.         return flag;  
  158.     }  
  159.   
  160.     public boolean createDirectory() {  
  161.         boolean flag = false;  
  162.           
  163.         // 创建FTP客户端   
  164.         FTPClient ftpClient = new FTPClient();  
  165.           
  166.         try {  
  167.             // 建立FTP连接   
  168.             ftpClient.connect(this.ftpServerIP);  
  169.             // 如果登录成功后, 才进行操作   
  170.             if (ftpClient.login(this.ftpName, this.ftpPassword)) {  
  171.                   
  172.                 // 切换文件路径, 到FTP上的"NNDD3"文件夹下   
  173.                 if (this.ftpPath != null && this.ftpPath.compareTo("") != 0  
  174.                         && ftpClient.changeWorkingDirectory(this.ftpPath)) {  
  175.                     SimpleDateFormat f = new SimpleDateFormat("yyyyMMdd");  
  176.                     String time = f.format(new Date());  
  177.                       
  178.                     String FolderName = time + "_ReTransmit";  
  179.                     ftpClient.makeDirectory(FolderName);  
  180.                     flag = true;  
  181.                 }  
  182.             }  
  183.         } catch (SocketException e) {  
  184.             e.printStackTrace();  
  185.             flag = false;  
  186.         } catch (IOException e) {  
  187.             e.printStackTrace();  
  188.             flag = false;  
  189.         } catch (Exception e) {  
  190.             e.printStackTrace();  
  191.             flag = false;  
  192.         } finally {  
  193.             try {  
  194.                 // 关闭连接   
  195.                 ftpClient.disconnect();  
  196.             } catch (IOException e) {  
  197.                 e.printStackTrace();  
  198.             }  
  199.         }  
  200.           
  201.         return flag;  
  202.     }  
  203.           
  204.     public String[] getAllFolderNames () {  
  205.         // 创建FTP客户端   
  206.         FTPClient ftpClient = new FTPClient();  
  207.           
  208.         try {  
  209.             // 建立FTP连接   
  210.             ftpClient.connect(this.ftpServerIP);  
  211.               
  212.             // 如果登录成功后, 才进行操作   
  213.             if (ftpClient.login(this.ftpName, this.ftpPassword)) {  
  214.                   
  215.                 // 切换文件路径, 到FTP上的"NNDD3"文件夹下   
  216.                 if (this.ftpPath != null && this.ftpPath.compareTo("") != 0  
  217.                         && ftpClient.changeWorkingDirectory(this.ftpPath)) {  
  218.                     // 将当前时间减去2天, 删除的是前两天的数据包   
  219.                     String time = minusTime();  
  220.           
  221.                     String[] allNames = ftpClient.listNames();  
  222.                       
  223.                     String[] temp = new String[allNames.length];  
  224.                     // 初始化数组   
  225.                     for (int j = 0; j < allNames.length; j ++) {  
  226.                         temp[j] = "";  
  227.                     }  
  228.                       
  229.                     // 找出要删除文件夹的名称   
  230.                     for (int i = 0; i < allNames.length; i ++) {  
  231.                         if (allNames[i].substring(08).compareTo(time) <= 0) {  
  232.                             temp[i] = allNames[i];  
  233.                         }  
  234.                     }  
  235.                       
  236.                     return temp;  
  237.                 }  
  238.             }  
  239.         } catch (SocketException e) {  
  240.             e.printStackTrace();  
  241.         } catch (IOException e) {  
  242.             e.printStackTrace();  
  243.         } finally {  
  244.             try {  
  245.                 // 关闭连接   
  246.                 ftpClient.disconnect();  
  247.             } catch (IOException e) {  
  248.                 e.printStackTrace();  
  249.             }  
  250.         }  
  251.           
  252.         return null;  
  253.     }  
  254.   
  255.     /** 
  256.      *  
  257.      * Method name: minusTime <BR> 
  258.      * Description: 获取钱两天的时间,如2011-8-1的前两天就是2011-7-30 <BR> 
  259.      * Remark: <BR> 
  260.      * @return  String<BR> 
  261.      */  
  262.     private String minusTime() {  
  263.         SimpleDateFormat df=new SimpleDateFormat("yyyyMMdd");     
  264.         Date d = new Date();  
  265.         String timeMinus2 = df.format(new Date(d.getTime() - 2 * 24 * 60 * 60 * 1000));     
  266.         return timeMinus2;  
  267.     }  
  268.       
  269.     public static void main(String[] args) {  
  270.         FTPFileTransmit ftpFileTransmit = new FTPFileTransmit();  
  271.         ftpFileTransmit.deleteFoldersInFTP();  
  272.           
  273. //      boolean flag = ftpFileTransmit.createDirectory();   
  274. //      if (flag) {   
  275. //          System.out.println("****** FTP文件夹创建成功 ******");   
  276. //      }   
  277.           
  278. //      String FolderName = ftpFileTransmit.ftpPath + "20110809_ReTransmit/";   
  279. //      byte[] data = new byte[1024];   
  280. //      for (int i = 0; i < data.length; i ++) {   
  281. //          data[i] = 'a';   
  282. //      }   
  283. //      boolean flag = ftpFileTransmit.saveInFTP(FolderName, "2011080912345678" ,data);   
  284. //      if (flag) {   
  285. //          System.out.println("****** FTP文件夹创建成功 ******");   
  286. //      }   
  287.     }  
  288. }  

相关内容