FTPClient

工具类:

package com.azcSoft.zbksyj.task.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.SocketException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class FtpClientUtil implements IFtpClient {
/**
* 客户端代理
*/
    private static FTPClient ftpClient; 
    /**
    * 连接ftp Server
    * @param userName ..
    * @param password ..
    * @param ip ..
    * @param port ..
    * @return boolean ..
     * @throws Exception 
    */
   public boolean connectServer(String userName,String password,String ip,int port) throws Exception   {
        boolean flag = true;
        /**接收异常信息.**/
            int reply;
            try {
            if(ftpClient == null){
            ftpClient = new FTPClient();
            }            
                /**文件名乱码,默认ISO8859-1,不支持中文 */
                ftpClient.setControlEncoding("GBK"); 
                ftpClient.connect(ip,port);
                ftpClient.login(userName, password);
              
                reply = ftpClient.getReplyCode();
                /**连接超时设置*/
                ftpClient.setDataTimeout(120000);
                if (!FTPReply.isPositiveCompletion(reply)) {
                    ftpClient.disconnect();
                    flag = false;
                }
            } catch (SocketException e) {
                flag = false;
                throw new Exception(e);
            } catch (IOException e) {
                flag = false;
                throw new Exception(e);
            }       
          return flag;
    }
   
   /**
    * 关闭ftp server
    * @throws IOException 
    */
   public void closeConnect() throws IOException  {
        
              if (ftpClient != null) {
                  ftpClient.logout();
                  ftpClient.disconnect();
              }                    
   }
   
   /**
    * 列出服务器指定路径下的文件目录
    * @param path ..
    * @return List ..
    * @throws IOException 
    */
   public  List<String> listRemoteAllFiles(String path) throws IOException  {
    List<String> list = new ArrayList<String>();
FTPFile[] files = ftpClient.listFiles(path);
for(FTPFile file:files){
if(!file.getName().equals(".")&& !file.getName().equals("..")){/**去掉 . ..*/
list.add(file.getName());
}  
        }
return list;
    }
   
   /**
    * 列出服务器指定路径下的文件目录
    * @param path ..
    * @param fileName ..
    * @return boolean ..
    * @throws IOException 
    */
   public  boolean ifExistFile(String path,String fileName) throws IOException  {
    String ftpFileName = "";
    boolean flag = false;  
FTPFile[] files = ftpClient.listFiles(path);
for(FTPFile file:files){
if(!file.getName().equals(".")&& !file.getName().equals("..")){
ftpFileName = file.getName();
if(fileName.equals(ftpFileName)){
flag = true;
}
}  
        }
return flag;
    }
   
   /**
    * 是否层级遍历
    * @param file ..
    * @param path ..
    * @throws IOException 
    */
   public void disFile(FTPFile file,String path) throws IOException {
      if(file.isDirectory() && !file.getName().equals(".")&& !file.getName().equals("..")){
       listRemoteAllFiles(File.separator +file.getName());
      }
  
    }
    
   
   /**
    * ftp 文件上传
    * @param remotePath ftp Server端 文件存放目录
    * @param remoteFilename 上传文件名
    * @param localPath 传文件本地地址
    * @return boolean ..
    * 
    */
   public boolean upFile(String remotePath, String remoteFilename,String localPath){
    boolean flag = true;                  
    FTPFile[] files;
try {
files = ftpClient.listFiles(remotePath);
/**不存在该目录 创建该目录.**/
    if(files.length == 0){
    mkdir(remotePath);
    }          
    File file = new File(localPath);
    /**本地流.**/
    InputStream fis = new FileInputStream(file);
    /**切换至server端目录.**/
    ftpClient.changeWorkingDirectory(remotePath);
    ftpClient.storeFile(remoteFilename, fis);
} catch (IOException e) {
flag = false;
}            
   return flag;
   }
   
     
   /**
    * ftp 文件下载
    * @param remotePath ftp server 端地址
    * @param localPath 下载到本地的 目录
    * @return boolean ..
    */
   public boolean downFile(String remotePath,String localPath) {     
    boolean flag = true;
    FTPFile[] ftpFiles;
try {
   /**切换至server端目录.**/
    ftpClient.changeWorkingDirectory(remotePath);
    ftpFiles = ftpClient.listFiles();
    for(FTPFile file:ftpFiles){ 
       /**非文件夹.**/
    if(!file.getName().equals(".")&& !file.getName().equals("..")){
       /**本地指定目录.**/
    File localDir = new File(localPath);
                if(!localDir.exists()){
                flag =  localDir.mkdirs();
                }
                 /**本地对应server的文件名.**/
                String fileName = localDir + "//" + file.getName();
                File localFile = new File(fileName);
                flag = localFile.createNewFile();
                FileOutputStream fos = new FileOutputStream(localFile); 
                 /**只能下载非文件夹的文件.**/
                if(!file.isDirectory()){
                 /**写入本地.**/
                ftpClient.retrieveFile(file.getName(), fos);  // 
                }else{
                flag = false;
                }                 
                fos.close();  
    }                 
    }
} catch (IOException e) {
flag = false;
}
return flag;
    }
   
   
   /**
    * 在ftp server 创建目录
    * @param pathname ..
    * @return boolean ..
    */
   public boolean mkdir(String pathname) {  
       /**注意编码,如果不编码文件中文目录无法创建. **/ 
  boolean flag = true;
    String dir;
        try {
            dir = new String(pathname.getBytes(),ftpClient.getControlEncoding());
            try {
                return  ftpClient.makeDirectory(dir);
            } catch (IOException e) {
            flag = false;
            }
        } catch (UnsupportedEncodingException e) {
        flag = false ;
        }
       return flag;  
   }
}

 

posted @ 2013-09-13 13:44  roscee  阅读(1815)  评论(0编辑  收藏  举报