Java进行FTP操作

来源:互联网 发布:sql修改表中的数据 编辑:程序博客网 时间:2024/04/30 18:08

1.Java进行FTP操作所需要的jar包

[java] view plaincopyprint?
  1. import org.apache.commons.io.IOUtils;  
  2. import org.apache.commons.net.ftp.FTPClient;  

2.上传文件到FTP

[java] view plaincopyprint?
  1.     public boolean saveInFTP (String reTransmitFolderName, String reTransmitFileName, byte[] data) {  
  2.         boolean flag = false;  
  3.           
  4.         // 创建FTP客户端  
  5.         FTPClient ftpClient = new FTPClient();  
  6.         // 输入流用于读取文件  
  7. //      FileInputStream fis = null;  
  8.         ByteArrayInputStream bis = null;  
  9.           
  10.         try {  
  11.             if (reTransmitFolderName != null  
  12.                     && reTransmitFolderName.compareTo("") != 0  
  13.                     && reTransmitFileName != null  
  14.                     && reTransmitFileName.compareTo("") != 0) {  
  15.   
  16.                 // 建立FTP连接  
  17.                 ftpClient.connect(this.ftpServerIP);  
  18.                   
  19.                 // 如果登录成功后, 才进行创建输入流  
  20.                 if (ftpClient.login(this.ftpName, this.ftpPassword)) {  
  21. //                  File srcClientFile = new File("C:/ParseXML.xml");  
  22.                       
  23.                     // 实例化输入流  
  24. //                  fis = new FileInputStream(srcClientFile);  
  25.                       
  26.                     if (ftpClient.changeWorkingDirectory(reTransmitFolderName)) {  
  27.                         // 将byte[]写入到输入流中, 实例化  
  28.                         bis = new ByteArrayInputStream(data);  
  29.                           
  30.                         // 设置缓冲  
  31.                         ftpClient.setBufferSize(RetransmitStateDefine.BUFFER_SIZE);  
  32.                           
  33.                         // 设置文件类型(二进制类型)  
  34.                         if (ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE)) {  
  35.                             flag = ftpClient.storeFile(reTransmitFileName, bis);  
  36.                         }  
  37.                     }  
  38.                 }  
  39.             }  
  40.         } catch (SocketException e) {  
  41.             e.printStackTrace();  
  42.             flag = false;  
  43.         } catch (IOException e) {  
  44.             e.printStackTrace();  
  45.             flag = false;  
  46.         } catch (Exception e) {  
  47.             e.printStackTrace();  
  48.             flag = false;  
  49.         } finally {  
  50.             try {  
  51.                 // 关闭输入流  
  52.                 IOUtils.closeQuietly(bis);  
  53.                 // 关闭连接  
  54.                 ftpClient.disconnect();  
  55.             } catch (IOException e) {  
  56.                 e.printStackTrace();  
  57.             }  
  58.         }  
  59.           
  60.         return flag;  
  61.     }  

3.从FTP上读取文件

[java] view plaincopyprint?
  1. public boolean getFromFTP () {  
  2.     boolean flag = false;  
  3.       
  4.     // 创建FTP客户端  
  5.     FTPClient ftpClient = new FTPClient();  
  6.     // 用于输出文件  
  7.     FileOutputStream fos = null;  
  8.       
  9.     try {  
  10.         // 建立FTP连接  
  11.         ftpClient.connect(this.ftpServerIP);  
  12.         // 如果登录成功  
  13.         if (ftpClient.login(this.ftpName, this.ftpPassword)) {  
  14.             // FTP文件  
  15.             String distinationFile = "/xxx_xx/xxx/ParseXML_InFTP.xml";  
  16.             // 实例化输出流  
  17.             fos = new FileOutputStream("C:/ParseXML_InFTP.xml");  
  18.               
  19.             // 设置缓冲  
  20.             ftpClient.setBufferSize(RetransmitStateDefine.BUFFER_SIZE);  
  21.               
  22.             // 设置文件类型(二进制类型)  
  23.             if (ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE)) {  
  24.                 ftpClient.retrieveFile(distinationFile, fos);  
  25.                 flag = true;  
  26.             }  
  27.         }  
  28.     } catch (SocketException e) {  
  29.         e.printStackTrace();  
  30.         flag = false;  
  31.     } catch (IOException e) {  
  32.         e.printStackTrace();  
  33.         flag = false;  
  34.     } catch (Exception e) {  
  35.         e.printStackTrace();  
  36.         flag = false;  
  37.     } finally {  
  38.         try {  
  39.             // 关闭输出流  
  40.             IOUtils.closeQuietly(fos);  
  41.             // 关闭连接  
  42.             ftpClient.disconnect();  
  43.         } catch (IOException e) {  
  44.             e.printStackTrace();  
  45.         }  
  46.     }  
  47.       
  48.     return flag;  
  49. }  

转载自:http://blog.csdn.net/android_robot/article/details/7094393

0 0
原创粉丝点击