Ftp工具类

package com.fastech.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class FtpTool {
    Logger logger=LoggerFactory.getLogger(this.getClass());
    public static FTPClient ftp = null;
    /**
     * Description:登录FTP
     * @author mnn
     * @param url FTP服务器hostname
     * @param port FTP服务器端口
     * @param username FTP登录账号
     * @param password FTP登录密码
     * */
    public static boolean login(String url, int port, String username, String password) {
        boolean success = false;
         ftp = new FTPClient();
            int reply;
            try {
                ftp.connect(url, port);
                //如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
                ftp.login(username, password);//登录
                reply = ftp.getReplyCode();
                if (!FTPReply.isPositiveCompletion(reply)) {
                    System.err.println("FTP server refused connection.");
                } else {
                    System.out.println("login success!");
                }
                System.out.println("FTP login is successful");
            } catch (SocketException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            System.out.println("FTP login is :"+success);    
        return success;
    }
    /**
    * Description: 从FTP服务器下载文件
    * @author mnn
    * @param remotePath FTP服务器上的相对路径
    * @param fileName 要下载的文件名
    * @param localPath 下载后保存到本地的路径
    * @return
    */
    public static boolean downFile(String remotePath,String fileName,String localPath) {
        boolean success = false;
        try {
            ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录
            FTPFile[] fs = ftp.listFiles();
            for(FTPFile ff:fs){
                if(ff.getName().equals(fileName)){
                    File localFile = new File(localPath+"/"+ff.getName());
                    OutputStream is = new FileOutputStream(localFile);
                    ftp.retrieveFile(ff.getName(), is);
                    is.close();
                }
            }
            success = true;
        } catch (IOException e) {
                e.printStackTrace();
        }
        System.out.println("downFile is:"+success);
        return success;
    }
    /**
    * Description: 向FTP服务器上传文件
    * @author mnn
    * @param path FTP服务器保存目录
    * @param filename 上传到FTP服务器上的文件名
    * @param input 输入流
    * @return 成功返回true,否则返回false
    */
    public static boolean uploadFile(String path, String filename, InputStream input) {
        boolean success = false;
        try {
            ftp.changeWorkingDirectory(path);
            ftp.setBufferSize(1024);
            ftp.setControlEncoding("GBK");
            //设置文件类型(二进制)
            ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
            ftp.storeFile(filename, input);//上传文件
            input.close();
            success = true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("uploadFile is:"+success);
        return success;
    }
    /**
     * Description: 删除ftp上的文件
     * @author mnn
     * @param srcFname 要删除的文件路径
     * */
    public boolean removeFile(String srcFname) {
        boolean flag = false;
        if (ftp != null) {
            try {
                //ftp.cwd("synEnumTransInfoFile");
                //ftp.changeWorkingDirectory(url);
                flag = ftp.deleteFile(srcFname);
                System.out.println("delete success!");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return flag;
    }
    /**
     * Description 关闭连接
     * @author mnn
     * */
    public void closeCon() {
        if (ftp != null) {
            try {
                ftp.logout();
                if (ftp.isConnected()) {
                    ftp.disconnect();
                    logger.info("ftp logout"+ftp.isConnected());
                    ftp = null;
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

 

posted @ 2017-08-31 13:59  枫沫  阅读(222)  评论(0编辑  收藏  举报