向ftp服务端上传文件和文件夹-20151201

import java.io.File; 
import java.io.FileInputStream; 
import java.util.Map;

import org.apache.commons.net.ftp.FTPClient; 
import org.apache.commons.net.ftp.FTPReply;

import com.sitech.ismp.coll.busi.e2e.rule.AnalysisRule;

/**
* @author smj
* */
    public class upToFtp extends AnalysisRule {

public upToFtp(Map<String, String> params) {
super(params);
// TODO Auto-generated constructor stub
}

private  FTPClient ftp;   
        /**
         * Description: 向FTP服务器上传文件夹
         * @param path 上传到ftp服务器哪个路径下   
         * @param url  FTP服务器地址
         * @param port 端口号
         * @param username 用户名
         * @param password 密码
         * @author smj
         * @throws Exception
         */  
        private  boolean connect(String path,String url,int port,String username,String password) throws Exception {   
            boolean result = false;   
            ftp = new FTPClient();   
            int reply;   
            ftp.connect(url,port);   
            ftp.login(username,password);   
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);   
            reply = ftp.getReplyCode();   
            if (!FTPReply.isPositiveCompletion(reply)) {   
                ftp.disconnect();   
                return result;   
            }   
            boolean flag4=ftp.changeWorkingDirectory(path); 
            logger.info("切换工作目录:"+flag4+"所要切换的路径:"+path);
            result = true;   
            return result;   
        }   
        /**
         * 
         * @param file 上传的文件或文件夹
         * @throws Exception
         */
        private boolean upload(File file) throws Exception{   
            if(file.isDirectory()){        
                //ftp.makeDirectory(file.getName());             
                ftp.changeWorkingDirectory(file.getPath());   
                String[] files = file.list(); 
                logger.info("文件列表有:"+files.toString() );
                for (int i = 0; i < files.length; i++) {   
                    File file1 = new File(files[i]); 
                    logger.info("我端的文件名:"+file1.getName() + "\t"  );
                    if(file1.isDirectory()){   
                        upload(file1);   
                        ftp.changeToParentDirectory();   
                    }else{                 
                        File file2 = new File(files[i]);
                        logger.info("文件名:"+file2.getName() + "\t"  );
                        FileInputStream input = new FileInputStream(file1);   
                        boolean flag3= ftp.storeFile(file2.getName(), input);
                        logger.info("文件传输状态"+flag3);
                        input.close();                         
                    }              
                }   
            }else{   
                File file2 = new File(file.getPath());   
                FileInputStream input = new FileInputStream(file2);   
                ftp.storeFile(file2.getName(), input);   
                input.close();     
            }
return true;   
        }
       
        /**
         * 移动指定文件或文件夹(包括所有文件和子文件夹)
         * 
         * @param fromDir
         *            要移动的文件或文件夹
         * @param toDir
         *            目标文件夹
         * @throws Exception
         */  
        public void MoveFolderAndFileWithSelf(String from, String to) throws Exception { 
            try { 
                File dir = new File(from); 
               
                to +=  File.separator + dir.getName(); 
                File moveDir = new File(to); 
                if(dir.isDirectory()){ 
                    if (!moveDir.exists()) { 
                        moveDir.mkdirs(); 
                    } 
                }else{ 
                    File tofile = new File(to); 
                    dir.renameTo(tofile); 
                    return; 
                } 
                 
                //System.out.println("dir.isDirectory()"+dir.isDirectory()); 
                //System.out.println("dir.isFile():"+dir.isFile()); 
                 
             // 文件一览   
                File[] files = dir.listFiles(); 
                if (files == null) 
                    return; 
     
                // 文件移动 
                for (int i = 0; i < files.length; i++) { 
                    System.out.println("文件名:"+files[i].getName()); 
                    if (files[i].isDirectory()) { 
                        MoveFolderAndFileWithSelf(files[i].getPath(), to); 
                     // 成功,删除原文件 
                        files[i].delete(); 
                    } 
                    File moveFile = new File(moveDir.getPath() + File.separator + files[i].getName()); 
                 // 目标文件夹下存在的话,删除 
                    if (moveFile.exists()) { 
                        moveFile.delete(); 
                    } 
                    files[i].renameTo(moveFile); 
                } 
                //dir.delete(); 
            } catch (Exception e) { 
                throw e; 
            } 
        } 
       
        protected void excute()  {
        logger.info("upToFtp start........");
      try {
  boolean flag1=connect("/ibnmsftp", "10.145.206.133", 21, "ibnmsftp", "ibnmsftp#2012");
  logger.info("连接对端ftp状态为::"+flag1 + "\t"  );
          File file = new File("/iBNMSConsole/ismp/CRM_DATA/"); 
          logger.info("文件路径为:"+file.getPath() + "文件名字为:" +file.getName()+ "\t"  );
          boolean flag= upload(file);
          logger.info("文件上传状态为:"+flag + "\t"  );
          if(flag==true){
          MoveFolderAndFileWithSelf("/iBNMSConsole/ismp/CRM_DATA/","/iBNMSConsole/ismp/CRM_HIS/");
           }
          logger.info("upToFtp end........");
      } catch (Exception e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  logger.info("上传文件出错");
 
  }
      } 
    } 

你可能感兴趣的:(工作)