gpt4 book ai didi

java - 使用 jsch 将文件从 linux 服务器目录复制到其他目录

转载 作者:太空宇宙 更新时间:2023-11-04 04:54:38 25 4
gpt4 key购买 nike

我要复制文件/home/user/dir1/abc.txt/home/user/dir2/ 。我是 linux 和 jsch 的新手,所以请指导我将上述文件复制到目录中。我尝试使用以下代码,但它对我不起作用。

public static void main(String args[]){

String host = "192.168.11.90";
String user = "Smt";
String password = "ping";
String command = "./test/stable-uniconnect-server-0.179/bin/launcher run";
String src ="/home/Smt/abc/text1.txt";
String dest ="/home/Smt/abcd";
String cp = "mv -u /home/Smt/abc/text1.txt /home/Sumit/abcd/";
try{
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
// Create a JSch session to connect to the server
Session session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
// Establish the connection
session.connect();
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(cp);
channel.setErrStream(System.err);
System.out.println("Connected...");
String dir="abc";

session.disconnect();
}
catch(Exception e){
e.printStackTrace();
}
}

最佳答案

这是您程序的修改后的代码。它对我有用。根据您的需要替换源和目标文件夹位置。

 import java.util.Properties;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;


/**
* @author jangachary.s
*
*/
public class CpTest {

public static void main(String args[]) {
String host = "192.168.01.69";
String user = "username";
String password = "password";
// String command = "./test/stable-uniconnect-server-0.179/bin/launcher run";
String src = "srcFolder";
String dest = "destFolder";
String cp = "cp " + src + "/fileName " + dest;
try {
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();

// Create a JSch session to connect to the server
Session session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
// Establish the connection
session.connect();
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(cp);

channel.connect();

channel.setErrStream(System.err);
System.out.println("Connected...");
session.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
}

关于java - 使用 jsch 将文件从 linux 服务器目录复制到其他目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50461388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com