如何在JSch SFTP上获得报酬? [英] How to reput on JSch SFTP?

查看:176
本文介绍了如何在JSch SFTP上获得报酬?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSch将文件上传到SFTP。它可以工作,但有时在上传文件时关闭TCP连接,导致服务器上的截断文件。

I'm using JSch to upload files to a SFTP. It works but sometimes the TCP connection is shut down while a file is being uploaded resulting on a truncated file on the server.

我发现 reput 命令恢复上传。如何使用JSch发送reput命令?它甚至可能吗?

I found out that the reput command on SFTP servers resumes the upload. How can I send a reput command with JSch? Is it even possible?

这是我的代码:

public void upload(File file) throws Exception
{
    JSch jsch = new JSch();

    Session session = jsch.getSession(USER, HOST, PORT);

    session.setPassword(PASSWORD);

    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);

    session.connect();

    Channel channel=session.openChannel("sftp");
    channel.connect();
    ChannelSftp sftpChannel = (ChannelSftp)channel;


    sftpChannel.put(file.getAbsolutePath(), file.getName());

    channel.disconnect();
    session.disconnect();
}


推荐答案

我找到了办法。将put方法与RESUME参数一起使用:

I found a way. Use the "put" method with the RESUME parameter:

sftpChannel.put(file.getAbsolutePath(), file.getName(), ChannelSftp.RESUME);

我的代码变为:

public static void upload(File file, boolean retry) {
    try 
    {
        System.out.println("Uplodaing file " + file.getName());

        JSch jsch = new JSch();
        Session session = jsch.getSession(USER, HOST, PORT);
        session.setPassword(PASSWORD);

        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);

        session.connect();

        Channel channel = session.openChannel("sftp");
        channel.connect();
        ChannelSftp sftpChannel = (ChannelSftp) channel;

        if (!retry)
            sftpChannel.put(file.getAbsolutePath(), file.getName(), ChannelSftp.OVERWRITE);
        else
            sftpChannel.put(file.getAbsolutePath(), file.getName(), ChannelSftp.RESUME);

        channel.disconnect();
        session.disconnect();
    } 
    catch (Exception e) 
    {
        e.printStackTrace();
        upload(file, true);
    }

}

这篇关于如何在JSch SFTP上获得报酬?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆