java - 使用 JSCH 将文件从一个远程服务器发送到另一个使用 JSCH 的服务器
问题描述
我想将文件从我的第一个远程服务器发送到另一个:
public boolean uploadFile() throws JSchException, SftpException {
ChannelSftp channelSftpA = createChannelSftp();
ChannelSftp channelSftpB = createChannelSftp();
channelSftpA.connect();
channelSftpB.connect();
localFilePath = "/data/upload/readme.txt";
remoteFilePath = "/bingo/pdf/";
channelSftpA.cd(localFilePath);
channelSftpA.put(localFilePath + "readme.txt", remoteFilePath + "readme.txt");
但它不起作用。我应该把channelB.put
我的第一个channelA.put
?
解决方案
如果我正确理解您的问题,您的代码将从第三台服务器运行,为了传输文件,您应该从中获取文件server A
,然后放在server B
. 顺便说一下,您要下载和上传文件的用户应该有权访问指定的文件夹!
private boolean transferFile() throws JSchException, SftpException {
ChannelSftp channelSftpA = createChannelSftp();
ChannelSftp channelSftpB = createChannelSftp();
channelSftpA.connect();
channelSftpB.connect();
String fileName = "readme.txt";
String remoteFilePathFrom = "/folderFrom/";
String remoteFilePathTo = "/folderTo/";
InputStream srcInputStream = channelSftpA.get(remoteFilePathFrom + fileName);
channelSftpB.put(srcInputStream, remoteFilePathTo + fileName);
System.out.println("Transfer has been completed");
channelSftpA.exit();
channelSftpB.exit();
return true;
}
推荐阅读
- javascript - 将对象数组与 ES6 中的对象进行比较
- python - Pydantic:使用文字验证有区别的联合
- android - 尝试从空对象引用上的字段 'androidx.fragment.app.FragmentManager androidx.fragment.app.Fragment.mFragmentManager' 读取)
- php - 自动配置替换导入的服务
- sql - 当生成选择语句时,它给我奇怪的文本作为 > 选择数据时?
- okhttp - okhttp 支持 http2 中的连接池
- c# - 如何从 swagger.json 中删除反引号
- python - PermissionError:[Errno 13] 权限被拒绝:'/var/www/FlaskApp/FlaskApp/conversationLogs/42779251.json'
- clojure - 递归函数性能不佳
- excel - 如何按代号设置工作表以传递给 UDF?