gpt4 book ai didi

java - 如何使用SCP将文件内容直接从本地流对象推送到远程文件到远程文件

转载 作者:行者123 更新时间:2023-11-30 10:56:57 25 4
gpt4 key购买 nike

如何使用SCP将文件内容直接从本地流对象推送到远程文件,而不需要将文件临时存储在本地磁盘上?

目前我正在本地创建流并尝试直接推送到远程服务器它能够成功建立 session 但是 channel.isConnected 变得错误,远程文件没有被创建,请帮助。

    Runtime rt = Runtime.getRuntime();
BufferedWriter out= new BufferedWriter(new FileWriter(f));
Process proc = rt.exec(cmd);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(proc.getErrorStream()));

//Intiate SCP connection

JSch jsch=new JSch();
jsch.setConfig("StrictHostKeyChecking", "no");
Session session=jsch.getSession("userName", "scpServer", 22);
UserInfo ui=new MyUserInfo(); //Passing password from this class
session.setUserInfo(ui);
session.connect();

String rfile="/home/npachava/SCPTest/myTest.bson";
// exec 'scp -f rfile' remotely
String command="scp -f "+rfile;
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
System.out.println("session.getServerVersion()"+session.getServerVersion());;
System.out.println("session.isConnected() "+session.isConnected());
System.out.println("channel.isConnected() "+channel.isConnected());
// get I/O streams for remote scp
OutputStream outScp=channel.getOutputStream();
InputStream inScp=channel.getInputStream();

channel.connect();

// read the output from the command
System.out.println("standard output of the command:\n");
String s = null;
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
out.write(s);
byte[] b = s.getBytes();
System.out.println("Bytes Length"+b.length);
outScp.write(b);



}
System.out.println("OutPutSCP"+outScp);
// read any errors from the attempted command
System.out.println("standard error of the command (if any):\n");
while ((s = stdError.readLine()) != null) {
System.out.println(s);
}
out.close();
outScp.flush();
outScp.close();
channel.disconnect();
session.disconnect();
}

我指的是 http://www.jcraft.com/jsch/examples/ScpTo.java.html 中提到的示例

最佳答案

只需使用您的内存流而不是 FileInputStream

在这一行(ScpTo.java 代码,不是上面的代码)中,将您的“本地”流对象(例如 ByteArrayInputStream)分配给 fis,而不是创建 FileInputStream

fis=new FileInputStream(lfile);

确保您首先从头开始搜索您的流。

关于java - 如何使用SCP将文件内容直接从本地流对象推送到远程文件到远程文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32819178/

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