FtpClientUtil

/*

  • To change this template, choose Tools | Templates
  • and open the template in the editor.
    */
    package ftpTool;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.ConnectException;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import org.apache.commons.net.PrintCommandListener;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;

/**
*

  • @author King Chen
    */
    public class FtpClientUtil {

    private static final String server = “pxrdev3.paxar.com”;
    private static final int port = 21;
    private static final String username = “FTPOSIM2”;
    private static final String password = “oracle#1234”;
    private static FTPClient ftp;

    public void upload(String remoteFileName, String locaFileName) {
    FTPClient ftp = null;
    try {
    ftp = new FTPClient();
    ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
    //连接ftp服务器
    connect(ftp);
    //设置属性
    setProperty(ftp);
    //上传文件
    upload(ftp, remoteFileName, locaFileName);
    //退出
    logout(ftp);
    } catch (Exception e) {
    } finally {
    if (ftp.isConnected()) {
    try {
    ftp.disconnect();
    } catch (IOException f) {
    }
    }
    }

    }

    /**

    • 上传文件
    • @param remoteFileName 远程文件名称
    • @param locaFileName 本地文件名称
      */
      public void download(String remoteFileName, String locaFileName) {
      if (ftp == null) {
      try {
      ftp = new FTPClient();
      ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
      //连接ftp服务器
      connect(ftp);
      //设置属性
      setProperty(ftp);
      //下载文件
      download(ftp, remoteFileName, locaFileName);
      //退出
      logout(ftp);
      } catch (Exception ex) {
      Logger.getLogger(FtpClientUtil.class.getName()).log(Level.SEVERE, null, ex);
      }
      } else {
      try {
      download(ftp, remoteFileName, locaFileName);
      logout(ftp);
      } catch (Exception ex) {
      Logger.getLogger(FtpClientUtil.class.getName()).log(Level.SEVERE, null, ex);
      }
      }

    }

    private static void connect(FTPClient ftp) throws Exception {
    //连接服务器
    ftp.connect(server, port);
    int reply = ftp.getReplyCode();
    //是否连接成功
    if (!FTPReply.isPositiveCompletion(reply)) {
    throw new ConnectException(server + " 服务器拒绝连接");
    }
    //登陆
    if (!ftp.login(username, password)) {
    throw new ConnectException(“用户名或密码错误”);
    }
    }

    private static void setProperty(FTPClient ftp) throws Exception {
    ftp.enterLocalPassiveMode();
    //二进制传输,默认为ASCII
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    }

    private void upload(FTPClient ftp, String remoteFileName,
    String locaFileName) throws Exception {
    //上传
    BufferedInputStream input;
    input = new BufferedInputStream(new FileInputStream(locaFileName));

// input = new FileInputStream(locaFileName);
ftp.storeFile(remoteFileName, input);

    input.close();
}

private final Timer timer = new Timer();

private  void logout(final FTPClient ftp) throws Exception {

    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            try {
                ftp.noop();
                ftp.logout();
            } catch (IOException ex) {
                Logger.getLogger(FtpClientUtil.class.getName()).log(Level.SEVERE, null, ex);
            }finally{
                try {
                    ftp.disconnect();
                } catch (IOException ex) {
                    Logger.getLogger(FtpClientUtil.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }
    }, new Date(System.currentTimeMillis() + 10000));

}

/**
 * @param ftp
 * @param remoteFileName
 * @param locaFileName
 */
private static void download(FTPClient ftp, String remoteFileName,
        String locaFileName) throws Exception {

    BufferedOutputStream output = null;

    output = new BufferedOutputStream(new FileOutputStream(locaFileName));

    ftp.retrieveFile(remoteFileName, output);
    output.close();

}

/**
 * @param ftp
 * @param remotePathName
 */
private void mkdir(FTPClient ftp, String remotePathName) throws Exception {
    ftp.makeDirectory(remotePathName);
}

public boolean isExsits(String ftpPath) {
    if (ftp != null) {
        try {
            try {
                FTPFile[] files = ftp.listFiles(ftpPath);
                return files != null && files.length > 0;
            } catch (IOException ex) {
                Logger.getLogger(FtpClientUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
            logout(ftp);

        } catch (Exception ex) {
            Logger.getLogger(FtpClientUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isExsits(ftpPath);

    } else {
        try {
            ftp = new FTPClient();
            try {
                ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
                //连接ftp服务器
                connect(ftp);
                //设置属性
                setProperty(ftp);
                FTPFile[] files = ftp.listFiles(ftpPath);
                return files != null && files.length > 0;
            } catch (Exception ex) {
                Logger.getLogger(FtpClientUtil.class.getName()).log(Level.SEVERE, null, ex);
            }

        } catch (Exception ex) {
            Logger.getLogger(FtpClientUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
        return isExsits(ftpPath);
    }
}

public  void download2(String remoteFileName, String locaFileName) throws Exception {
    
    long time1=new Date().getTime();

    if (ftp != null) {
        connect(ftp);
        try {
            try {
                ftp.setBufferSize(8*1024*1024);
                FTPFile[] files = ftp.listFiles(remoteFileName);
                if (files != null && files.length > 0) {
                    download(ftp, remoteFileName, locaFileName);
                } else {
                    JOptionPane.showMessageDialog(null, "未找到你想要的数据");
                }
            } catch (IOException ex) {
                 
                Logger.getLogger(FtpClientUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
            logout(ftp);

        } catch (Exception ex) {
            Logger.getLogger(FtpClientUtil.class.getName()).log(Level.SEVERE, null, ex);
        }

    } 
    if (ftp == null){
        try {
            ftp = new FTPClient();
            ftp.setBufferSize(8*1024*1024);
            try {
                ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
                //连接ftp服务器
                connect(ftp);
                //设置属性
                setProperty(ftp);
                FTPFile[] files = ftp.listFiles(remoteFileName);
                if (files != null && files.length > 0) {
                    download(ftp, remoteFileName, locaFileName);
                } else {
                    JOptionPane.showMessageDialog(null, "未找到你想要的数据");
                }
            } catch (Exception ex) {
                Logger.getLogger(FtpClientUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
            logout(ftp);
        } catch (Exception ex) {
            Logger.getLogger(FtpClientUtil.class.getName()).log(Level.SEVERE, null, ex);
        }

    }
    
    long time2=new Date().getTime();
    
    int time3=(int) ((time2-time1)/1000);
    JOptionPane.showMessageDialog(null, time3);

}

public static boolean isFTPFileExist(String filename) {

    FTPClient ftp = new FTPClient();
    ftp.setBufferSize(8 * 1024);
    ftp.setDataTimeout(60000);    //设置传输超时时间为60秒 
    ftp.setConnectTimeout(60000);       //连接超时为60秒

    try {

        ftp.addProtocolCommandListener(new PrintCommandListener(new PrintWriter(System.out), true));
        connect(ftp);
        if (!FTPReply.isPositiveCompletion(ftp.getReplyCode())) {
            ftp.disconnect();
            return false;
        }

        ftp.enterLocalActiveMode();
        ftp.setFileType(FTP.BINARY_FILE_TYPE);
        ftp.setControlEncoding("GBK");

        String dir = "/u10/oracle/common/data/Item_attachment/PFL/PDF/";

        ftp.changeWorkingDirectory(dir);

        InputStream is = ftp.retrieveFileStream(new String(filename.getBytes("GBK"), FTP.DEFAULT_CONTROL_ENCODING));
        if (is == null || ftp.getReplyCode() == FTPReply.FILE_UNAVAILABLE) {
            return false;
        }
        
         long time1 = new Date().getTime();

        String remoteFileName = dir + filename;

        String locaFileName = "C:\\test\\" + filename;

        download(ftp, remoteFileName, locaFileName);
        long time2 = new Date().getTime();
        int timeprice = (int) ((time2 - time1) / 1000);
        JOptionPane.showMessageDialog(null, timeprice);
        
        

        if (is != null) {
          is.close();
            ftp.completePendingCommand();
        }

       

        return true;

    } catch (Exception ex) {
        Logger.getLogger(FtpClientUtil.class.getName()).log(Level.SEVERE, null, ex);
    } finally {      
            try {
                ftp.disconnect();
            } catch (IOException ex) {
                Logger.getLogger(FtpClientUtil.class.getName()).log(Level.SEVERE, null, ex);
            }
       

    }
    return false;
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值