ftp工具类

package com.ttg.utils.common;

//~--- non-JDK imports --------------------------------------------------------

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

import java.io.*;

//~--- JDK imports ------------------------------------------------------------

//~--- classes ----------------------------------------------------------------

/**
 * 
 */
public class FtpClientUtil {

    /**
     * Field ftp Description
     */
    private static FTPClient ftp;

    /**
     *
     * @param path
     *            上传到ftp服务器哪个路径下
     * @param addr
     *            地址
     * @param port
     *            端口号
     * @param username
     *            用户名
     * @param password
     *            密码
     * @return
     * @throws Exception
     */
    public static boolean connect(String path, String addr, int port, String username, String password) throws Exception {
    boolean result = false;

    ftp = new FTPClient();

    int reply;

    ftp.connect(addr, port);
    ftp.login(username, password);
    ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
    reply = ftp.getReplyCode();

    if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();

        return result;
    }

    ftp.changeWorkingDirectory(path);
    result = true;

    return result;
    }

    /**
     *
     * @param file
     *            上传的文件或文件夹
     * @throws Exception
     */
    public static void upload(File file) throws Exception {
    if (file.isDirectory()) {
        ftp.makeDirectory(file.getName());
        ftp.changeWorkingDirectory(file.getName());

        String[] files = file.list();

        for (int i = 0; i < files.length; i++) {
        File file1 = new File(file.getPath() + "\\" + files[i]);

        if (file1.isDirectory()) {
            upload(file1);
            ftp.changeToParentDirectory();
        } else {
            File file2 = new File(file.getPath() + "\\" + files[i]);
            FileInputStream input = new FileInputStream(file2);

            ftp.storeFile(file2.getName(), input);
            input.close();
        }
        }
    } else {
        File file2 = new File(file.getPath());
        FileInputStream input = new FileInputStream(file2);

        ftp.storeFile(file2.getName(), input);
        input.close();
    }
    }

    /**
     * Description: 向FTP服务器上传文件
     * 
     * @param url
     *            FTP服务器hostname
     * @param port
     *            FTP服务器端口
     * @param username
     *            FTP登录账号
     * @param password
     *            FTP登录密码
     * @param path
     *            FTP服务器保存目录
     * @param filename
     *            上传到FTP服务器上的文件名
     * @param input
     *            输入流
     * @return 成功返回true,否则返回false
     */
    public static boolean uploadFile(String url, int port, String username, String password, String path, String filename, InputStream input) {
    boolean success = false;
    ftp = new FTPClient();
    FTPClientConfig config = new FTPClientConfig();
    // config.setXXX(YYY); // change required options
    // for example config.setServerTimeZoneId("Pacific/Pitcairn")
    ftp.setDataTimeout(6000);
    ftp.setConnectTimeout(6000);
    ftp.configure(config);
    try {
        int reply;

        ftp.connect(url, port); // 连接FTP服务器

        // ftp.connect(url);
        // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
        ftp.login(username, password); // 登录
        reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();

        return success;
        }
        ftp.changeWorkingDirectory(path);
        ftp.storeFile(filename, input);
        input.close();
        ftp.logout();
        success = true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (ftp.isConnected()) {
        try {
            ftp.disconnect();
        } catch (IOException ioe) {
        }
        }
    }

    return success;
    }

    /**
     * Description: 从FTP服务器下载文件
     * 
     * @param url
     *            FTP服务器hostname
     * @param port
     *            FTP服务器端口
     * @param username
     *            FTP登录账号
     * @param password
     *            FTP登录密码
     * @param remotePath
     *            FTP服务器上的相对路径
     * @param fileName
     *            要下载的文件名
     * @param localPath
     *            下载后保存到本地的路径
     * @return
     */
    public static boolean downFile(String url, int port, String username, String password, String remotePath, String fileName, String localPath) {
    boolean success = false;
    FTPClient ftp = new FTPClient();

    try {
        int reply;

        ftp.connect(url, port);

        // 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
        ftp.login(username, password); // 登录
        reply = ftp.getReplyCode();

        if (!FTPReply.isPositiveCompletion(reply)) {
        ftp.disconnect();

        return success;
        }

        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();
        }
        }

        ftp.logout();
        success = true;
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (ftp.isConnected()) {
        try {
            ftp.disconnect();
        } catch (IOException ioe) {
        }
        }
    }

    return success;
    }
}

 

posted @ 2016-12-14 17:11  余欣  阅读(1954)  评论(0编辑  收藏  举报