Copying a file in sftp with jsch library

巧了我就是萌 提交于 2019-12-19 05:48:30

问题


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();
    }
}

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

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..


回答1:


Do a copy of the remote file and then delete it

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


来源:https://stackoverflow.com/questions/4826821/copying-a-file-in-sftp-with-jsch-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!