Java实现连接FTP服务并传递文件

简介: Java实现连接FTP服务并传递文件

public class FtpClientUtil {

private String host;

private int port;

private String username;

private String password;

private int bufferSize = 10 * 1024 * 1024;

private int soTimeout = 15000;

private FTPClient ftp;

public FTPClient getFtp() {
    return ftp;
}

public void setFtp(FTPClient ftp) {
    this.ftp = ftp;
}

private UploadStatus uploadStatus;

public UploadStatus getUploadStatus() {
    return uploadStatus;
}

public void setUploadStatus(UploadStatus uploadStatus) {
    this.uploadStatus = uploadStatus;
}

public static class Builder {
    private String host;
    private int port = 21;
    private String username;
    private String password;
    private int bufferSize = 1024 * 1024;
    private FTPClientConfig config;
    private int defaultTimeout = 15000;
    private int connectTimeout = 15000;
    private int dataTimeout = 15000;
    private int controlKeepAliveTimeout = 300;
    private int soTimeout = 15000;

    public Builder() {
    }

    public Builder host(String host) {
        this.host = host;
        return this;
    }

    public Builder port(int port) {
        this.port = port;
        return this;
    }

    public Builder username(String username) {
        this.username = username;
        return this;
    }

    public Builder password(String password) {
        this.password = password;
        return this;
    }

    public Builder bufferSize(int bufferSize) {
        this.bufferSize = bufferSize;
        return this;
    }

    public Builder config(FTPClientConfig config) {
        this.config = config;
        return this;
    }

    public Builder defaultTimeout(int defaultTimeout) {
        this.defaultTimeout = defaultTimeout;
        return this;
    }

    public Builder connectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
        return this;
    }

    public Builder dataTimeout(int dataTimeout) {
        this.dataTimeout = dataTimeout;
        return this;
    }

    public Builder soTimeout(int soTimeout) {
        this.soTimeout = soTimeout;
        return this;
    }

    public Builder controlKeepAliveTimeout(int controlKeepAliveTimeout) {
        this.controlKeepAliveTimeout = controlKeepAliveTimeout;
        return this;
    }

    public FtpClientUtil build() throws IOException {
        FtpClientUtil instance = new FtpClientUtil(this.host, this.port, this.username, this.password,
                this.bufferSize, this.config, this.defaultTimeout, this.dataTimeout, this.connectTimeout,
                this.controlKeepAliveTimeout, this.soTimeout);
        return instance;
    }
}

private FtpClientUtil(String host, int port, String username, String password, int bufferSize,
        FTPClientConfig config, int defaultTimeout, int dataTimeout, int connectTimeout,
        int controlKeepAliveTimeout, int soTimeout) throws IOException {
    this.host = host;
    this.port = port;
    this.username = username;
    this.password = password;
    this.bufferSize = bufferSize;
    this.soTimeout = soTimeout;
    this.ftp = new FTPClient();
    if (config != null) {
        this.ftp.configure(config);
    }
    ftp.setControlEncoding("UTF-8");

// ftp.setControlEncoding("GBK");
// ftp.setControlEncoding("gb2312");

    ftp.enterLocalPassiveMode();
    ftp.setDefaultTimeout(defaultTimeout);
    ftp.setConnectTimeout(connectTimeout);
    ftp.setDataTimeout(dataTimeout);
    // ftp.setSendDataSocketBufferSize(1024 * 256);
    if (this.bufferSize > 0) {
        ftp.setBufferSize(this.bufferSize);
    }

    // keeping the control connection alive
    ftp.setControlKeepAliveTimeout(controlKeepAliveTimeout);// 每大约5分钟发一次noop,防止大文件传输导致的控制连接中断
}

public FtpClientUtil connect() throws SocketException, IOException {
    if (!this.ftp.isConnected()) {
        this.ftp.connect(this.host, this.port);
        int reply = this.ftp.getReplyCode();
        if (!FTPReply.isPositiveCompletion(reply)) {
            logger.warn("ftp服务器返回码[{}], 连接失败...", reply);
            throw new IllegalStateException("连接ftp服务器失败,返回的状态码是" + reply);
        }
    }
    this.ftp.setSoTimeout(this.soTimeout);
    return this;
}

public FtpClientUtil login() throws IOException {
    boolean suc = this.ftp.login(this.username, this.password);
    if (!suc) {
        throw new IllegalStateException("登录ftp服务器失败");
    }
    return this;
}

/**
 * ftp上传文件功能
 * 
 * @param file
 *            要上传的文件
 * @param relativePath
 *            要上传到ftp服务器的相对路径
 * @return
 * @throws IOException
 */
public FtpClientUtil upload(File file, String relativePath) throws IOException {
    FileInputStream fInputStream = new FileInputStream(file);
    return this.upload(fInputStream, file.getName(), relativePath, file.length());
}

public FtpClientUtil upload(InputStream inputStream, String name, String relativePath, long localSize)
        throws IOException {
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    changeWorkingDirectory(relativePath);

    this.ftp.enterLocalPassiveMode();
    FTPFile[] listFiles = this.ftp.listFiles(name);
    // long localSize = inputStream.available();// ? 不知道好用否
    if (listFiles.length == 1) {
        long remoteSize = listFiles[0].getSize();

        if (remoteSize == localSize) {
            this.setUploadStatus(UploadStatus.File_Exits);
            return this;
        } else if (remoteSize > localSize) {
            this.setUploadStatus(UploadStatus.Remote_Bigger_Local);
            return this;
        }
        this.uploadFile(inputStream, name, remoteSize, localSize);
    } else {
        this.uploadFile(inputStream, name, 0, localSize);
    }
    logger.info("{}/{} upload success", relativePath, name);
    return this;
}

private void uploadFile(InputStream inputStream, String name, long remoteSize, long localSize) throws IOException {
    this.ftp.enterLocalPassiveMode();
    OutputStream output = null;
    long step = localSize / 100;
    long process = 0;
    long localreadbytes = 0L;
    try {
        if (remoteSize > 0) {
            output = this.ftp.appendFileStream(name);
            this.ftp.setRestartOffset(remoteSize);
            inputStream.skip(remoteSize);
            process = remoteSize / step;
            localreadbytes = remoteSize;
        } else {
            output = this.ftp.storeFileStream(name);
        }
        byte[] bytes = new byte[1024];
        int c;
        while ((c = inputStream.read(bytes)) != -1) {
            output.write(bytes, 0, c);
            localreadbytes += c;
            if (localreadbytes / step >= process + 10) {
                process = localreadbytes / step;
                logger.info("文件【" + name + "】上传ftp进度汇报, process = " + process);
            }
        }
        logger.info("文件" + name + "上传ftp进度汇报, process = " + 100);
        output.flush();
        inputStream.close();
        output.close();
        boolean result = this.ftp.completePendingCommand();
        if (remoteSize > 0) {
            this.setUploadStatus(
                    result ? UploadStatus.Upload_From_Break_Success : UploadStatus.Upload_From_Break_Failed);
        } else {
            this.setUploadStatus(
                    result ? UploadStatus.Upload_New_File_Success : UploadStatus.Upload_New_File_Failed);
        }
    } catch (Exception e) {
        this.setUploadStatus(
                remoteSize > 0 ? UploadStatus.Upload_From_Break_Failed : UploadStatus.Upload_New_File_Failed);
    }

}

public OutputStream upload(String name, String relativePath) throws IOException {
    ftp.setFileType(FTP.BINARY_FILE_TYPE);
    changeWorkingDirectory(relativePath);
    ftp.enterLocalPassiveMode();
    return this.ftp.storeFileStream(name);
}

public void changeWorkingDirectory(String relativePath) throws IOException {
    if (relativePath == null) {
        throw new NullPointerException("relativePath can't be null");
    }
    String[] dirs = relativePath.split("/");
    for (String dir : dirs) {
        if (!this.ftp.changeWorkingDirectory(dir)) {
            if (this.ftp.makeDirectory(dir)) {
                this.ftp.changeWorkingDirectory(dir);
            } else {
                logger.warn("{}目录创建失败, 导致不能进入合适的目录进行上传", dir);
            }
        }
    }
}

/**
 * ftp上传目录下所有文件的功能
 * 
 * @param file
 *            要上传的目录
 * @param relativePath
 *            要上传到ftp服务器的相对路径
 * @return
 * @throws IOException
 */
public FtpClientUtil uploadDir(File file, String relativePath) throws IOException {
    if (!file.isDirectory()) {
        throw new IllegalArgumentException("file argument is not a directory!");
    }
    relativePath = relativePath + "/" + file.getName();
    File[] listFiles = file.listFiles();
    for (File f : listFiles) {
        this.uploadFree(f, relativePath);
    }
    return this;
}

/**
 * ftp上传文件, 调用方不用区分文件是否为目录,由该方法自己区分处理
 * 
 * @param file
 *            要上传的文件
 * @param relativePath
 *            要上传到ftp服务器的相对路径
 * @return
 * @throws IOException
 */
public FtpClientUtil uploadFree(File file, String relativePath) throws IOException {
    if (file.isDirectory()) {
        this.uploadDir(file, relativePath);
    } else {
        this.upload(file, relativePath);
    }
    return this;
}

/**
 * 本方法是上传的快捷方法,方法中自身包含了ftp 连接、登陆、上传、退出、断开各个步骤
 * 
 * @param file
 *            要上传的文件
 * @param relativePath
 *            要上传到ftp服务器的相对路径
 */
public boolean uploadOneStep(File file, String relativePath) {
    try {
        this.connect().login().uploadFree(file, relativePath);
        return true;
    } catch (IOException e) {
        String msg = String.format("ftp上传时发生异常, filename = [%s], relativePath = [%s]", file.getName(),
                relativePath);
        logger.error(msg, e);
        return false;
    } finally {
        this.disconnectFinally();
    }
}

public boolean uploadOneStepForStream(InputStream inputStram, String name, String relativePath, long localSize) {
    try {
        this.connect().login().upload(inputStram, name, relativePath, localSize);
        return true;
    } catch (IOException e) {
        String msg = String.format("ftp上传时发生异常, filename = [%s], relativePath = [%s]", name, relativePath);
        logger.error(msg, e);
        return false;
    } finally {
        this.disconnectFinally();
    }
}

public interface OutputStreamForUpload {
    public void write(OutputStream outputStream) throws IOException;
}

public boolean uploadOneStepForStream(OutputStreamForUpload outputUpload, String name, String relativePath) {
    try {
        this.connect().login();
        OutputStream upload = this.upload(name, relativePath);
        outputUpload.write(upload);
        return true;
    } catch (IOException e) {
        String msg = String.format("ftp上传时发生异常, filename = [%s], relativePath = [%s]", name, relativePath);
        logger.error(msg, e);
        return false;
    } finally {
        this.disconnectFinally();
    }
}

public FtpClientUtil logout() throws IOException {
    this.ftp.logout();
    return this;
}

public void disconnect() {
    this.disconnectFinally();
}

private void disconnectFinally() {
    if (this.ftp.isConnected()) {
        try {
            this.ftp.disconnect();
        } catch (IOException ioe) {
            logger.warn("ftp断开服务器链接异常", ioe);
        }
    }
}

@Override
public String toString() {
    return "FtpClientHelper [host=" + host + ", port=" + port + ", username=" + username + ", password=" + password
            + "]";
}

}
看代码吧:

相关文章
|
Java Unix Go
【Java】(8)Stream流、文件File相关操作,IO的含义与运用
Java 为 I/O 提供了强大的而灵活的支持,使其更广泛地应用到文件传输和网络编程中。!但本节讲述最基本的和流与 I/O 相关的功能。我们将通过一个个例子来学习这些功能。
518 1
|
JSON Java 数据格式
java调用服务报错400
java调用服务报错400
318 6
java调用服务报错400
|
JSON Java 数据格式
java调用服务报错415 Content type ‘application/octet-stream‘ not supported
java调用服务报错415 Content type ‘application/octet-stream‘ not supported
694 6
|
存储 数据采集 搜索推荐
Java 大视界 -- Java 大数据在智慧文旅旅游景区游客情感分析与服务改进中的应用实践(226)
本篇文章探讨了 Java 大数据在智慧文旅景区中的创新应用,重点分析了如何通过数据采集、情感分析与可视化等技术,挖掘游客情感需求,进而优化景区服务。文章结合实际案例,展示了 Java 在数据处理与智能推荐等方面的强大能力,为文旅行业的智慧化升级提供了可行路径。
Java 大视界 -- Java 大数据在智慧文旅旅游景区游客情感分析与服务改进中的应用实践(226)
|
分布式计算 搜索推荐 算法
Java 大视界 -- Java 大数据在智慧养老服务需求分析与个性化服务匹配中的应用(186)
本篇文章探讨了Java大数据技术在智慧养老服务需求分析与个性化服务匹配中的应用。通过整合老年人健康数据与行为数据,结合机器学习与推荐算法,实现对老年人健康风险的预测及个性化服务推荐,提升养老服务的智能化与精准化水平,助力智慧养老高质量发展。
|
存储 Java 编译器
深入理解Java虚拟机--类文件结构
本内容介绍了Java虚拟机与Class文件的关系及其内部结构。Class文件是一种与语言无关的二进制格式,包含JVM指令集、符号表等信息。无论使用何种语言,只要能生成符合规范的Class文件,即可在JVM上运行。文章详细解析了Class文件的组成,包括魔数、版本号、常量池、访问标志、类索引、字段表、方法表和属性表等,并说明其在Java编译与运行过程中的作用。
492 0
|
存储 人工智能 Java
java之通过Http下载文件
本文介绍了使用Java实现通过文件链接下载文件到本地的方法,主要涉及URL、HttpURLConnection及输入输出流的操作。
956 0
|
监控 Java API
Java语言按文件创建日期排序及获取最新文件的技术
这段代码实现了文件创建时间的读取、文件列表的获取与排序以及获取最新文件的需求。它具备良好的效率和可读性,对于绝大多数处理文件属性相关的需求来说足够健壮。在实际应用中,根据具体情况,可能还需要进一步处理如访问权限不足、文件系统不支持某些属性等边界情况。
703 14
|
JSON 网络协议 安全
【Java】(10)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
784 1