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
            + "]";
}

}
看代码吧:

相关文章
|
3天前
|
监控 Java 应用服务中间件
高级java面试---spring.factories文件的解析源码API机制
【11月更文挑战第20天】Spring Boot是一个用于快速构建基于Spring框架的应用程序的开源框架。它通过自动配置、起步依赖和内嵌服务器等特性,极大地简化了Spring应用的开发和部署过程。本文将深入探讨Spring Boot的背景历史、业务场景、功能点以及底层原理,并通过Java代码手写模拟Spring Boot的启动过程,特别是spring.factories文件的解析源码API机制。
15 2
|
6天前
|
存储 缓存 安全
在 Java 编程中,创建临时文件用于存储临时数据或进行临时操作非常常见
在 Java 编程中,创建临时文件用于存储临时数据或进行临时操作非常常见。本文介绍了使用 `File.createTempFile` 方法和自定义创建临时文件的两种方式,详细探讨了它们的使用场景和注意事项,包括数据缓存、文件上传下载和日志记录等。强调了清理临时文件、确保文件名唯一性和合理设置文件权限的重要性。
18 2
|
15天前
|
存储 安全 Java
如何保证 Java 类文件的安全性?
Java类文件的安全性可以通过多种方式保障,如使用数字签名验证类文件的完整性和来源,利用安全管理器和安全策略限制类文件的权限,以及通过加密技术保护类文件在传输过程中的安全。
|
16天前
|
SQL Java 数据库连接
在Java应用中,数据库访问常成为性能瓶颈。连接池技术通过预建立并复用数据库连接,有效减少连接开销,提升访问效率
在Java应用中,数据库访问常成为性能瓶颈。连接池技术通过预建立并复用数据库连接,有效减少连接开销,提升访问效率。本文介绍了连接池的工作原理、优势及实现方法,并提供了HikariCP的示例代码。
30 3
|
17天前
|
存储 Java API
Java实现导出多个excel表打包到zip文件中,供客户端另存为窗口下载
Java实现导出多个excel表打包到zip文件中,供客户端另存为窗口下载
25 4
|
16天前
|
Java 数据库连接 数据库
深入探讨Java连接池技术如何通过复用数据库连接、减少连接建立和断开的开销,从而显著提升系统性能
在Java应用开发中,数据库操作常成为性能瓶颈。本文通过问题解答形式,深入探讨Java连接池技术如何通过复用数据库连接、减少连接建立和断开的开销,从而显著提升系统性能。文章介绍了连接池的优势、选择和使用方法,以及优化配置的技巧。
16 1
|
16天前
|
Java 数据库连接 数据库
Java连接池在数据库性能优化中的重要作用。连接池通过预先创建和管理数据库连接,避免了频繁创建和关闭连接的开销
本文深入探讨了Java连接池在数据库性能优化中的重要作用。连接池通过预先创建和管理数据库连接,避免了频繁创建和关闭连接的开销,显著提升了系统的响应速度和吞吐量。文章介绍了连接池的工作原理,并以HikariCP为例,展示了如何在Java应用中使用连接池。通过合理配置和优化,连接池技术能够有效提升应用性能。
32 1
|
存储 设计模式 缓存
听说你还不懂 Java 的服务定位器模式(Service Locator Pattern)?(上)
听说你还不懂 Java 的服务定位器模式(Service Locator Pattern)?
201 0
听说你还不懂 Java 的服务定位器模式(Service Locator Pattern)?(上)
|
8天前
|
安全 Java 测试技术
Java并行流陷阱:为什么指定线程池可能是个坏主意
本文探讨了Java并行流的使用陷阱,尤其是指定线程池的问题。文章分析了并行流的设计思想,指出了指定线程池的弊端,并提供了使用CompletableFuture等替代方案。同时,介绍了Parallel Collector库在处理阻塞任务时的优势和特点。
|
4天前
|
安全 Java 开发者
深入解读JAVA多线程:wait()、notify()、notifyAll()的奥秘
在Java多线程编程中,`wait()`、`notify()`和`notifyAll()`方法是实现线程间通信和同步的关键机制。这些方法定义在`java.lang.Object`类中,每个Java对象都可以作为线程间通信的媒介。本文将详细解析这三个方法的使用方法和最佳实践,帮助开发者更高效地进行多线程编程。 示例代码展示了如何在同步方法中使用这些方法,确保线程安全和高效的通信。
23 9