Class Ftp

All Implemented Interfaces:
Closeable, AutoCloseable

public class Ftp extends AbstractFtp
FTP客户端封装 此客户端基于Apache-Commons-Net
Since:
Java 17+
Author:
Kimi Liu
  • Field Details

  • Constructor Details

    • Ftp

      public Ftp(String host)
      构造,匿名登录
      Parameters:
      host - 域名或IP
    • Ftp

      public Ftp(String host, int port)
      构造,匿名登录
      Parameters:
      host - 域名或IP
      port - 端口
    • Ftp

      public Ftp(String host, int port, String user, String password)
      构造
      Parameters:
      host - 域名或IP
      port - 端口
      user - 用户名
      password - 密码
    • Ftp

      public Ftp(String host, int port, String user, String password, Charset charset)
      构造
      Parameters:
      host - 域名或IP
      port - 端口
      user - 用户名
      password - 密码
      charset - 编码
    • Ftp

      public Ftp(String host, int port, String user, String password, Charset charset, String serverLanguageCode, String systemKey)
      构造
      Parameters:
      host - 域名或IP
      port - 端口
      user - 用户名
      password - 密码
      charset - 编码
      serverLanguageCode - 服务器语言 例如:zh
      systemKey - 服务器标识 例如:org.apache.commons.net.ftp.FTPClientConfig.SYST_NT
    • Ftp

      public Ftp(String host, int port, String user, String password, Charset charset, String serverLanguageCode, String systemKey, FtpMode mode)
      构造
      Parameters:
      host - 域名或IP
      port - 端口
      user - 用户名
      password - 密码
      charset - 编码
      serverLanguageCode - 服务器语言
      systemKey - 系统关键字
      mode - 模式
    • Ftp

      public Ftp(FtpConfig config, FtpMode mode)
      构造
      Parameters:
      config - FTP配置
      mode - 模式
    • Ftp

      public Ftp(org.apache.commons.net.ftp.FTPClient client)
      构造
      Parameters:
      client - 自定义实例化好的FTPClient
  • Method Details

    • init

      public Ftp init()
      初始化连接
      Returns:
      this
    • init

      public Ftp init(String host, int port, String user, String password)
      初始化连接
      Parameters:
      host - 域名或IP
      port - 端口
      user - 用户名
      password - 密码
      Returns:
      this
    • init

      public Ftp init(String host, int port, String user, String password, FtpMode mode)
      初始化连接
      Parameters:
      host - 域名或IP
      port - 端口
      user - 用户名
      password - 密码
      mode - 模式
      Returns:
      this
    • init

      public Ftp init(FtpConfig config, FtpMode mode)
      初始化连接
      Parameters:
      config - FTP配置
      mode - 模式
      Returns:
      this
    • setMode

      public Ftp setMode(FtpMode mode)
      设置FTP连接模式,可选主动和被动模式
      Parameters:
      mode - 模式枚举
      Returns:
      this
    • isBackToPwd

      public boolean isBackToPwd()
      是否执行完操作返回当前目录
      Returns:
      执行完操作是否返回当前目录
    • setBackToPwd

      public Ftp setBackToPwd(boolean backToPwd)
      设置执行完操作是否返回当前目录
      Parameters:
      backToPwd - 执行完操作是否返回当前目录
      Returns:
      this
    • reconnectIfTimeout

      public Ftp reconnectIfTimeout()
      如果连接超时的话,重新进行连接 经测试,当连接超时时,client.isConnected()仍然返回ture,无法判断是否连接超时 因此,通过发送pwd命令的方式,检查连接是否超时
      Specified by:
      reconnectIfTimeout in class AbstractFtp
      Returns:
      this
    • cd

      public boolean cd(String directory)
      改变目录
      Specified by:
      cd in class AbstractFtp
      Parameters:
      directory - 目录
      Returns:
      是否成功
    • pwd

      public String pwd()
      远程当前目录
      Specified by:
      pwd in class AbstractFtp
      Returns:
      远程当前目录
    • ls

      public List<String> ls(String path)
      Description copied from class: AbstractFtp
      遍历某个目录下所有文件和目录,不会递归遍历
      Specified by:
      ls in class AbstractFtp
      Parameters:
      path - 需要遍历的目录
      Returns:
      文件和目录列表
    • lsFiles

      public List<org.apache.commons.net.ftp.FTPFile> lsFiles(String path, Predicate<org.apache.commons.net.ftp.FTPFile> filter)
      遍历某个目录下所有文件和目录,不会递归遍历 此方法自动过滤"."和".."两种目录
      Parameters:
      path - 目录
      filter - 过滤器,null表示不过滤,默认去掉"."和".."两种目录
      Returns:
      文件或目录列表
    • lsFiles

      public org.apache.commons.net.ftp.FTPFile[] lsFiles(String path) throws org.aoju.bus.core.exception.InternalException
      遍历某个目录下所有文件和目录,不会递归遍历
      Parameters:
      path - 目录,如果目录不存在,抛出异常
      Returns:
      文件或目录列表
      Throws:
      org.aoju.bus.core.exception.InternalException - 路径不存在
      org.aoju.bus.core.exception.InternalException - IO异常
    • mkdir

      public boolean mkdir(String dir)
      Description copied from class: AbstractFtp
      在当前远程目录(工作目录)下创建新的目录
      Specified by:
      mkdir in class AbstractFtp
      Parameters:
      dir - 目录名
      Returns:
      是否创建成功
    • existFile

      public boolean existFile(String path)
      判断ftp服务器目录内是否还有子元素(目录或文件)
      Parameters:
      path - 文件路径
      Returns:
      是否存在
    • stat

      public int stat(String path)
      获取服务端目录状态。
      Parameters:
      path - 路径
      Returns:
      状态int,服务端不同,返回不同
    • delFile

      public boolean delFile(String path)
      Description copied from class: AbstractFtp
      删除指定目录下的指定文件
      Specified by:
      delFile in class AbstractFtp
      Parameters:
      path - 目录路径
      Returns:
      是否存在
    • delDir

      public boolean delDir(String dirPath)
      Description copied from class: AbstractFtp
      删除文件夹及其文件夹下的所有文件
      Specified by:
      delDir in class AbstractFtp
      Parameters:
      dirPath - 文件夹路径
      Returns:
      boolean 是否删除成功
    • upload

      public boolean upload(String dest, File file)
      上传文件到指定目录,可选:
       1. dest为null或""上传到当前路径
       2. dest为相对路径则相对于当前路径的子路径
       3. dest为绝对路径则上传到此路径
       
      Specified by:
      upload in class AbstractFtp
      Parameters:
      dest - 服务端路径,可以为null 或者相对路径或绝对路径
      file - 文件
      Returns:
      是否上传成功
    • upload

      public boolean upload(String dest, String fileName, File file)
      上传文件到指定目录,可选:
       1. dest为null或""上传到当前路径
       2. dest为相对路径则相对于当前路径的子路径
       3. dest为绝对路径则上传到此路径
       
      Parameters:
      dest - 服务端路径,可以为null 或者相对路径或绝对路径
      fileName - 自定义在服务端保存的文件名
      file - 文件
      Returns:
      是否上传成功
    • upload

      public boolean upload(String dest, String fileName, InputStream fileStream)
      上传文件到指定目录,可选:
       1. path为null或""上传到当前路径
       2. path为相对路径则相对于当前路径的子路径
       3. path为绝对路径则上传到此路径
       
      Parameters:
      dest - 服务端路径,可以为null 或者相对路径或绝对路径
      fileName - 文件名
      fileStream - 文件流
      Returns:
      是否上传成功
    • download

      public void download(String path, File outFile)
      下载文件
      Specified by:
      download in class AbstractFtp
      Parameters:
      path - 文件路径,包含文件名
      outFile - 输出文件或目录,当为目录时,使用服务端的文件名
    • download

      public void download(String path, String fileName, File outFile)
      下载文件
      Parameters:
      path - 文件所在路径(远程目录),不包含文件名
      fileName - 文件名
      outFile - 输出文件或目录,当为目录时使用服务端文件名
    • download

      public void download(String path, String fileName, OutputStream out)
      下载文件到输出流
      Parameters:
      path - 文件路径
      fileName - 文件名
      out - 输出位置
    • download

      public void download(String path, String fileName, OutputStream out, Charset charset)
      下载文件到输出流
      Parameters:
      path - 服务端的文件路径
      fileName - 服务端的文件名
      out - 输出流,下载的文件写出到这个流中
      charset - 文件名编码,通过此编码转换文件名编码为ISO8859-1
    • download

      public void download(String sourcePath, String desPath)
      递归获取远程文件
      Specified by:
      download in class AbstractFtp
      Parameters:
      sourcePath - 服务器目录
      desPath - 本地目录
    • getClient

      public org.apache.commons.net.ftp.FTPClient getClient()
      获取FTPClient客户端对象
      Returns:
      FTPClient
    • close

      public void close() throws IOException
      Throws:
      IOException