posts - 166,  comments - 29,  trackbacks - 0

package com.sinovatech.unicom.efb.common;


import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.SocketException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

/**
 *
 * 目的:FTP文件类
 * <br/>
 *
 * FTP上传文件、下载文件
 *
 * *
 */
public class FTPUtil {
 private static final Log log = LogFactory.getLog(FTPUtil.class);

 public static int download(String ip,int port,String userName,String password,FileFilter filter,String localFilePath) throws SocketException, IOException{
  return download(ip, port, userName, password, null,filter, localFilePath);
 }
 
 public static int download(String ip,int port,String userName,String password,String ftpPath,FileFilter filter,String localFilePath) throws SocketException, IOException{
  FTPClient ftp = new FTPClient();
  int i=0;
   try {
    // 不为空连接 通过IP 和端口
    ftp.connect(ip, port);
    // 登陆
    if (ftp.login(userName, password)) {
     ftp.enterLocalPassiveMode();
     // 获取FTP登陆目录下的所有文件
     if(ftpPath!=null){
      if(!ftp.changeWorkingDirectory(ftpPath)){
       throw new RuntimeException("找不到该目录:"+ftpPath);
      }
     }
     FTPFile[] files = ftp.listFiles();
     for (FTPFile file : files) {
      String fileName = file.getName();
      if (filter.accept(fileName)) {
       BufferedOutputStream out = null;
       try {
        // IO流下载文件到本地
        out = new BufferedOutputStream(
          new FileOutputStream(new File(localFilePath
            ,fileName)));
        // 开始下载
        ftp.retrieveFile(file.getName(), out);
        log.info("下载文件:" + file.getName() + "到本地路径:"
          + localFilePath);
        i++;
       } finally {
        try {
         if (out != null) {
          out.close();
         }
        } catch (Exception e) {
         log.error("", e);
        }
       }
      }
     }
    } else {
     i = -1;
    }
   } finally {
    if(ftp != null&&ftp.isConnected()){
     try {
      log.info("关闭ftp连接");
      ftp.disconnect();
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
   }
  return i;
 }
 
 public static int download(String ip,int port,String userName,String password,String ftpPath,final String fileName,String localFilePath)throws SocketException, IOException{
  
 
  return  download(ip, port, userName, password,ftpPath,new FileFilter(){

    public boolean accept(String fileName1) {
   
     return fileName.equalsIgnoreCase(fileName1);
    }
    
   }, localFilePath);
 
 }
 
 
 public static int download(String ip,int port,String userName,String password,final String fileName,String localFilePath)throws SocketException, IOException{
  return  download(ip, port, userName, password,null,fileName, localFilePath);
 }

 
 /**
  * ftp上传文件至服务器
  * @throws SocketException
  * @throws IOException
  */
 public static void uploadFile(String ip, int port, String userName, String password, String localFile)
       throws SocketException, IOException{
  log.info("上传本地文件: " + localFile);
  File file = new File(localFile);
  InputStream in = null;
  FTPClient ftpClient = null;
  if(file != null){
   try {
    ftpClient = new FTPClient();
    in = new FileInputStream(file);
    ftpClient.connect(ip,port);
    if(ftpClient.login(userName, password)){
     ftpClient.enterLocalPassiveMode();
     boolean flag= ftpClient.appendFile(file.getName(), in);
     log.info("上传文件成功:" + flag);
    }
   }catch (SocketException e) {
    log.error("ftp上传文件失败:", e);
    throw e;
   }catch (IOException e) {
    log.error("ftp上传文件失败:", e);
    throw e;
   } finally{
    if(in != null){
     try {
      in.close();
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
    if(ftpClient != null){
     try {
      ftpClient.disconnect();
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
   }
  }
 }
 
 
 public static void upload(String ip, int port, String userName, String password, String path,String remotePath)
 throws SocketException, IOException{
  upload(ip, port, userName, password, new File(path), remotePath);
 }
 
 /**
  * ftp上传文件至服务器
  * @throws SocketException
  * @throws IOException
  */
 public static void upload(String ip, int port, String userName, String password, File localFile,String remotePath)
       throws SocketException, IOException{
  log.info("上传本地文件: " + localFile.getName());
  InputStream in = null;
  FTPClient ftpClient = null;
  if(localFile != null){
   try {
    ftpClient = new FTPClient();
    in = new FileInputStream(localFile);
    ftpClient.connect(ip,port);
    if(ftpClient.login(userName, password)){
     ftpClient.enterLocalPassiveMode();
     if(remotePath!=null){
      if(!ftpClient.changeWorkingDirectory(remotePath)){
       ftpClient.makeDirectory(remotePath);
       ftpClient.changeWorkingDirectory(remotePath);
      }
     }
     boolean flag= ftpClient.storeFile(localFile.getName(), in);
     log.info("上传文件成功:" + flag);
    }
   }catch (SocketException e) {
    log.error("ftp上传文件失败:", e);
    throw e;
   }catch (IOException e) {
    log.error("ftp上传文件失败:", e);
    throw e;
   } finally{
    if(in != null){
     try {
      in.close();
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
    if(ftpClient != null){
     try {
      ftpClient.disconnect();
     } catch (IOException e) {
      e.printStackTrace();
     }
    }
   }
  }
 }

}

posted on 2011-01-21 12:01 David1228 阅读(2302) 评论(0)  编辑  收藏 所属分类: J2EE

只有注册用户登录后才能发表评论。


网站导航:
 

<2011年1月>
2627282930311
2345678
9101112131415
16171819202122
23242526272829
303112345

常用链接

留言簿(3)

随笔分类

随笔档案

文章档案

新闻分类

新闻档案

相册

收藏夹

Java

Linux知识相关

Spring相关

云计算/Linux/虚拟化技术/

友情博客

多线程并发编程

开源技术

持久层技术相关

搜索

  •  

积分与排名

  • 积分 - 269109
  • 排名 - 180

最新评论

阅读排行榜

评论排行榜