使用jsch库复制sftp中的文件 [英] Copying a file in sftp with jsch library

查看:238
本文介绍了使用jsch库复制sftp中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

import com.jcraft.jsch.*;

public class App {
public static void main(String args[]) {
    JSch jsch = new JSch();
    Session session = null;
    try {
        session = jsch.getSession("Username", "Host", PORT NO);
        session.setConfig("StrictHostKeyChecking", "no");
        session.setPassword("Password");
        session.connect();

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

        sftpChannel.get("remotefile.txt", "localfile.txt");
        sftpChannel.exit();
        session.disconnect();
    } catch (JSchException e) {
        e.printStackTrace(); 
    } catch (SftpException e) {
        e.printStackTrace();
    }
}

我不想要这个sftpChannel.get(remotefile。 txt,localfile.txt);

I dont want this sftpChannel.get("remotefile.txt", "localfile.txt");

我只想创建两个方法
1)将文件从远程位置复制到本地系统
2)在sftp连接中删除复制的文件

I just want to create two methods 1)to copy the file from remote location to local system 2)to remove the copied file in an sftp connection

任何人都可以提供帮助..

Can anyone help..

推荐答案

执行远程文件的副本然后将其删除

Do a copy of the remote file and then delete it

ChannelSftp.get("remotefile.txt", "localfile.txt");
ChannelSftp.rm("remotefile.txt")

这篇关于使用jsch库复制sftp中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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