Class JschSftp

java.lang.Object
org.miaixz.bus.extra.ftp.AbstractFtp
org.miaixz.bus.extra.ssh.provider.jsch.JschSftp
All Implemented Interfaces:
Closeable, AutoCloseable, Ftp

public class JschSftp extends AbstractFtp
SFTP是Secure File Transfer Protocol的缩写,安全文件传送协议。可以为传输文件提供一种安全的加密方法。 SFTP 为 SSH的一部份,是一种传输文件到服务器的安全方式。SFTP是使用加密传输认证信息和传输的数据,所以,使用SFTP是非常安全的。 但是,由于这种传输方式使用了加密/解密技术,所以传输效率比普通的FTP要低得多,如果您对网络安全性要求更高时,可以使用SFTP代替FTP。

此类为基于jsch的SFTP实现 参考:https://www.cnblogs.com/longyg/archive/2012/06/25/2556576.html

Since:
Java 17+
Author:
Kimi Liu
  • Constructor Details

    • JschSftp

      public JschSftp(FtpConfig config)
      构造
      Parameters:
      config - FTP配置
    • JschSftp

      public JschSftp(FtpConfig config, boolean init)
      构造
      Parameters:
      config - FTP配置
      init - 是否立即初始化
    • JschSftp

      public JschSftp(com.jcraft.jsch.Session session, Charset charset, long timeOut)
      构造
      Parameters:
      session - Session
      charset - 编码
      timeOut - 超时时间,单位毫秒
    • JschSftp

      public JschSftp(com.jcraft.jsch.ChannelSftp channel, Charset charset, long timeOut)
      构造
      Parameters:
      channel - ChannelSftp
      charset - 编码
      timeOut - 超时时间,单位毫秒
  • Method Details

    • of

      public static JschSftp of(String sshHost, int sshPort, String sshUser, String sshPass)
      构造
      Parameters:
      sshHost - 远程主机
      sshPort - 远程主机端口
      sshUser - 远程主机用户名
      sshPass - 远程主机密码
      Returns:
      JschSftp
    • of

      public static JschSftp of(String sshHost, int sshPort, String sshUser, String sshPass, Charset charset)
      构造
      Parameters:
      sshHost - 远程主机
      sshPort - 远程主机端口
      sshUser - 远程主机用户名
      sshPass - 远程主机密码
      charset - 编码
      Returns:
      JschSftp
    • init

      public void init()
      初始化
    • reconnectIfTimeout

      public JschSftp reconnectIfTimeout()
      Description copied from interface: Ftp
      如果连接超时的话,重新进行连接
      Returns:
      this
    • getClient

      public com.jcraft.jsch.ChannelSftp getClient()
      获取SFTP通道客户端
      Returns:
      通道客户端
    • pwd

      public String pwd()
      远程当前目录
      Returns:
      远程当前目录
    • home

      public String home()
      获取HOME路径
      Returns:
      HOME路径
    • ls

      public List<String> ls(String path)
      遍历某个目录下所有文件或目录,不会递归遍历
      Parameters:
      path - 遍历某个目录下所有文件或目录
      Returns:
      目录或文件名列表
    • lsDirs

      public List<String> lsDirs(String path)
      遍历某个目录下所有目录,不会递归遍历
      Parameters:
      path - 遍历某个目录下所有目录
      Returns:
      目录名列表
    • lsFiles

      public List<String> lsFiles(String path)
      遍历某个目录下所有文件,不会递归遍历
      Parameters:
      path - 遍历某个目录下所有文件
      Returns:
      文件名列表
    • ls

      public List<String> ls(String path, Predicate<com.jcraft.jsch.ChannelSftp.LsEntry> predicate)
      遍历某个目录下所有文件或目录,不会递归遍历 此方法自动过滤"."和".."两种目录
      Parameters:
      path - 遍历某个目录下所有文件或目录
      predicate - 文件或目录过滤器,可以实现过滤器返回自己需要的文件或目录名列表,Predicate.test(Object)true保留
      Returns:
      目录或文件名列表
    • lsEntries

      public List<com.jcraft.jsch.ChannelSftp.LsEntry> lsEntries(String path)
      遍历某个目录下所有文件或目录,生成LsEntry列表,不会递归遍历 此方法自动过滤"."和".."两种目录
      Parameters:
      path - 遍历某个目录下所有文件或目录
      Returns:
      目录或文件名列表
    • lsEntries

      public List<com.jcraft.jsch.ChannelSftp.LsEntry> lsEntries(String path, Predicate<com.jcraft.jsch.ChannelSftp.LsEntry> predicate)
      遍历某个目录下所有文件或目录,生成LsEntry列表,不会递归遍历 此方法自动过滤"."和".."两种目录
      Parameters:
      path - 遍历某个目录下所有文件或目录
      predicate - 文件或目录过滤器,可以实现过滤器返回自己需要的文件或目录名列表,Predicate.test(Object)true保留
      Returns:
      目录或文件名列表
    • mkdir

      public boolean mkdir(String dir)
      Description copied from interface: Ftp
      在当前远程目录(工作目录)下创建新的目录
      Parameters:
      dir - 目录名
      Returns:
      是否创建成功
    • isDir

      public boolean isDir(String dir)
      Description copied from interface: Ftp
      判断给定路径是否为目录
      Parameters:
      dir - 被判断的路径
      Returns:
      是否为目录
    • cd

      public boolean cd(String directory) throws org.miaixz.bus.core.lang.exception.InternalException
      打开指定目录,如果指定路径非目录或不存在抛出异常
      Parameters:
      directory - directory
      Returns:
      是否打开目录
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException - 进入目录失败异常
    • delFile

      public boolean delFile(String filePath)
      删除文件
      Parameters:
      filePath - 要删除的文件绝对路径
      Returns:
      是否存在
    • delDir

      public boolean delDir(String dirPath)
      删除文件夹及其文件夹下的所有文件
      Parameters:
      dirPath - 文件夹路径
      Returns:
      boolean 是否删除成功
    • upload

      public void upload(String remotePath, File file)
      将本地文件或者文件夹同步(覆盖)上传到远程路径
      Parameters:
      remotePath - 远程路径
      file - 文件或者文件夹
    • uploadFile

      public boolean uploadFile(String destPath, File file)
      Description copied from interface: Ftp
      将本地文件上传到目标服务器,目标文件名为destPath,若destPath为目录,则目标文件名将与file文件名相同。 覆盖模式
      Parameters:
      destPath - 服务端路径,可以为null 或者相对路径或绝对路径
      file - 需要上传的文件
      Returns:
      是否成功
    • uploadFile

      public void uploadFile(String destPath, String fileName, InputStream fileStream)
      上传文件到指定目录,可选:
       1. path为null或""上传到当前路径
       2. path为相对路径则相对于当前路径的子路径
       3. path为绝对路径则上传到此路径
       
      Parameters:
      destPath - 服务端路径,可以为null 或者相对路径或绝对路径
      fileName - 文件名
      fileStream - 文件流
    • put

      public JschSftp put(String srcFilePath, String destPath)
      将本地文件上传到目标服务器,目标文件名为destPath,若destPath为目录,则目标文件名将与srcFilePath文件名相同。覆盖模式
      Parameters:
      srcFilePath - 本地文件路径
      destPath - 目标路径,
      Returns:
      this
    • put

      public JschSftp put(String srcFilePath, String destPath, JschSftp.Mode mode)
      将本地文件上传到目标服务器,目标文件名为destPath,若destPath为目录,则目标文件名将与srcFilePath文件名相同。
      Parameters:
      srcFilePath - 本地文件路径
      destPath - 目标路径,
      mode - JschSftp.Mode 模式
      Returns:
      this
    • put

      public JschSftp put(String srcFilePath, String destPath, com.jcraft.jsch.SftpProgressMonitor monitor, JschSftp.Mode mode)
      将本地文件上传到目标服务器,目标文件名为destPath,若destPath为目录,则目标文件名将与srcFilePath文件名相同。
      Parameters:
      srcFilePath - 本地文件路径
      destPath - 目标路径,
      monitor - 上传进度监控,通过实现此接口完成进度显示
      mode - JschSftp.Mode 模式
      Returns:
      this
    • put

      public JschSftp put(InputStream srcStream, String destPath, com.jcraft.jsch.SftpProgressMonitor monitor, JschSftp.Mode mode)
      将本地数据流上传到目标服务器,目标文件名为destPath,目标必须为文件
      Parameters:
      srcStream - 本地的数据流
      destPath - 目标路径,
      monitor - 上传进度监控,通过实现此接口完成进度显示
      mode - JschSftp.Mode 模式
      Returns:
      this
    • download

      public void download(String src, File destFile)
      Description copied from interface: Ftp
      下载文件
      Parameters:
      src - 文件路径
      destFile - 输出文件或目录
    • download

      public void download(String src, OutputStream out)
      下载文件到OutputStream
      Parameters:
      src - 源文件路径,包括文件名
      out - 目标流
      See Also:
    • recursiveDownloadFolder

      public void recursiveDownloadFolder(String sourcePath, File destDir) throws org.miaixz.bus.core.lang.exception.InternalException
      递归下载FTP服务器上文件到本地(文件目录和服务器同步)
      Parameters:
      sourcePath - ftp服务器目录,必须为目录
      destDir - 本地目录
      Throws:
      org.miaixz.bus.core.lang.exception.InternalException
    • get

      public JschSftp get(String src, String dest)
      获取远程文件
      Parameters:
      src - 远程文件路径
      dest - 目标文件路径
      Returns:
      this
    • get

      public JschSftp get(String src, OutputStream out)
      获取远程文件
      Parameters:
      src - 远程文件路径
      out - 目标流
      Returns:
      this
    • getFileStream

      public InputStream getFileStream(String path)
      Description copied from interface: Ftp
      读取FTP服务器上的文件为输入流
      Parameters:
      path - 文件路径
      Returns:
      InputStream
    • close

      public void close()
    • toString

      public String toString()
      Overrides:
      toString in class Object