JavaFX如何停止当前的SFTP文件传输。

huangapple go评论70阅读模式
英文:

JavaFX How to stop current transfer file SFTP

问题

我想停止当前的文件传输,使用方法 stopUpload()

private ChannelSftp channelSftp;

private ChannelSftp setupJsch() throws JSchException {
    JSch jsch = new JSch();
    jsch.setKnownHosts("/Users/john/.ssh/known_hosts");
    Session jschSession = jsch.getSession(username, remoteHost);
    jschSession.setPassword(password);
    jschSession.connect();
    return (ChannelSftp) jschSession.openChannel("sftp");
}

public void stopUpload()
{
    channelSftp.disconnect();
}

public void whenUploadFileUsingJsch_thenSuccess() throws JSchException, SftpException {
    ChannelSftp channelSftp = setupJsch();
    channelSftp.connect();

    String localFile = "src/main/resources/sample.txt";
    String remoteDir = "remote_sftp_test/";

    channelSftp.put(localFile, remoteDir + "jschFile.txt");
    channelSftp.exit();
}

当运行 `stopUpload()`我遇到了这个错误:`Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException`
英文:

I would like to stop my current transfer file with using method stopUpload() :

private ChannelSftp channelSftp

private ChannelSftp setupJsch() throws JSchException {
    JSch jsch = new JSch();
    jsch.setKnownHosts("/Users/john/.ssh/known_hosts");
    Session jschSession = jsch.getSession(username, remoteHost);
    jschSession.setPassword(password);
    jschSession.connect();
    return (ChannelSftp) jschSession.openChannel("sftp");
}

public void stopUpload()
{

   channelSftp.disconnect();

}

public void whenUploadFileUsingJsch_thenSuccess() throws JSchException, SftpException {
    ChannelSftp channelSftp = setupJsch();
    channelSftp.connect();
 
    String localFile = "src/main/resources/sample.txt";
    String remoteDir = "remote_sftp_test/";
 
    channelSftp.put(localFile, remoteDir + "jschFile.txt");
    channelSftp.exit();
}

When stopUpload() run I have this error : Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException

答案1

得分: 2

为了在需要时清理地取消 JSch 的 SFTP 传输,可以实现 SftpProgressMonitor 接口

public class CancellableProgressMonitor implements SftpProgressMonitor {
    private boolean cancelled;

    public CancellableProgressMonitor() {}

    public void cancel() {
        this.cancelled = true;
    }

    public boolean wasCancelled() {
        return this.cancelled;
    }

    public void init(int op, java.lang.String src, java.lang.String dest, long max) {
        this.cancelled = false;
    }

    public boolean count(long bytes) {
        return !this.cancelled;
    }

    public void end() {
    }
}

然后将它传递给 ChannelSftp.put 方法:

CancellableProgressMonitor monitor = new CancellableProgressMonitor();
channelSftp.put(localFile, remoteDir + "jschFile.txt", monitor);

在需要取消传输时调用 monitor.cancel() 方法:

public void stopUpload() {
    monitor.cancel();
}

如果你想清理部分传输的文件:

String remoteFile = remoteDir + "jschFile.txt";
try {
    channelSftp.put(localFile, remoteFile, monitor);
} catch (IOException e) {
    if (monitor.wasCancelled() && channelSftp.getSession().isConnected()) {
        try {
            channelSftp.rm(remoteFile);
        } catch (SftpException e) {
            if (e.id == SSH_FX_NO_SUCH_FILE) {
                // 如果在文件甚至被创建之前传输被取消,可能会发生这种情况
            } else {
                throw e;
            }
        }
    }

    throw e;
}
英文:

To cleanly cancel a JSch SFTP transfer, when you need, implement the SftpProgressMonitor interface:

public class CancellableProgressMonitor implements SftpProgressMonitor {
    private boolean cancelled;

    public CancellableProgressMonitor() {}

    public void cancel() {
        this.cancelled = true;
    }

    public bool wasCancelled() {
        return this.cancelled;
    }

    public void init(int op, java.lang.String src, java.lang.String dest, long max) {
        this.cancelled = false;
    }

    public boolean count(long bytes) {
        return !this.cancelled;
    }

    public void end() {
    }
}

And pass it to ChannelSftp.put:

CancellableProgressMonitor monitor = new CancellableProgressMonitor()
channelSftp.put(localFile, remoteDir + "jschFile.txt", monitor);

Call monitor.cancel() when you need to cancel the transfer.

public void stopUpload() {
    monitor.cancel();
}

If you want to cleanup the partially transferred file:

String remoteFile = remoteDir + "jschFile.txt";
try {
    channelSftp.put(localFile, remoteFile, monitor);
} catch (IOException e) {
    if (monitor.wasCancelled() && channelSftp.getSession().isConnected()) {
        try {
            channelSftp.rm(remoteFile);
        } catch (SftpException e) {
            if (e.id == SSH_FX_NO_SUCH_FILE) {
                // can happen if the transfer was cancelled 
                // before the file was even created
            } else {
                throw e;
            }
        }
    }

    throw e;
}

huangapple
  • 本文由 发表于 2020年9月22日 20:29:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64009763.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定