You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Grails 2.4.5项目中simpleSFTP无法检测文件存在的替代方案咨询

可行的SFTP库及实现方案

1. 直接使用JSch(推荐,轻量且兼容现有插件底层)

simpleSFTP插件本身基于JSch实现,你可以直接引入JSch依赖,自己封装文件存在检测的方法,或者扩展现有插件功能:

步骤:

  • 在BuildConfig.groovy中添加JSch依赖:
dependencies {
    compile 'com.jcraft:jsch:0.1.55'
}
  • 封装工具方法检测文件存在:
import com.jcraft.jsch.ChannelSftp
import com.jcraft.jsch.JSch
import com.jcraft.jsch.Session

boolean isFileExists(String host, int port, String username, String password, String remoteFilePath) {
    Session session = null
    ChannelSftp channel = null
    try {
        JSch jsch = new JSch()
        session = jsch.getSession(username, host, port)
        session.setPassword(password)
        session.setConfig("StrictHostKeyChecking", "no")
        session.connect()
        
        channel = (ChannelSftp) session.openChannel("sftp")
        channel.connect()
        
        // 尝试获取文件属性,不存在会抛出SftpException
        channel.stat(remoteFilePath)
        return true
    } catch (com.jcraft.jsch.SftpException e) {
        // 错误码2对应文件不存在
        if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
            return false
        }
        // 其他异常按需处理
        throw e
    } finally {
        if (channel != null && channel.isConnected()) {
            channel.disconnect()
        }
        if (session != null && session.isConnected()) {
            session.disconnect()
        }
    }
}

2. 使用Apache Commons VFS(封装更完善,统一文件系统API)

Apache Commons VFS提供了跨文件系统的统一操作接口,原生支持SFTP的文件存在检测:

步骤:

  • 在BuildConfig.groovy中添加依赖:
dependencies {
    compile 'org.apache.commons:commons-vfs2:2.1'
}
  • 实现文件检测逻辑:
import org.apache.commons.vfs2.FileObject
import org.apache.commons.vfs2.FileSystemManager
import org.apache.commons.vfs2.VFS

boolean isFileExists(String host, int port, String username, String password, String remoteFilePath) {
    FileSystemManager fsManager = VFS.getManager()
    String sftpUrl = "sftp://${username}:${password}@${host}:${port}${remoteFilePath}"
    FileObject file = fsManager.resolveFile(sftpUrl)
    try {
        return file.exists()
    } finally {
        file.close()
    }
}

3. 扩展simpleSFTP插件(无需替换现有依赖)

如果不想更换插件,可以直接扩展它的服务类,利用插件内部已有的JSch连接实例:

示例:

import grails.plugins.simplesftp.SimpleSftpService
import com.jcraft.jsch.ChannelSftp

class CustomSftpService {
    SimpleSftpService simpleSftpService

    boolean isFileExists(String remoteFilePath) {
        try {
            // 获取插件已建立的ChannelSftp连接
            ChannelSftp channel = simpleSftpService.channel
            channel.stat(remoteFilePath)
            return true
        } catch (com.jcraft.jsch.SftpException e) {
            if (e.id == ChannelSftp.SSH_FX_NO_SUCH_FILE) {
                return false
            }
            throw e
        }
    }
}

注意:调用此方法前需确保插件已通过connect()建立好SFTP连接。

内容的提问来源于stack exchange,提问作者Simon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 05:03:18