我有运行ssh服务器的Windows机器。我知道那台机器上的路径。设为D:/Folder1/Folder2
。我正在使用JSCH创建sftp通道。但是当我尝试cd到D:/Folder1/Folder2
时,会抛出"No such file: 2"
错误。
任何人都可以帮忙吗?可能我需要转换路径吗?
答案 0 :(得分:0)
我通过打开exec频道使用ChannelExec解决了这个问题。这对我有用。希望也适用于其他人。
...
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch ssh = new JSch();
session = ssh.getSession(sshSolrUsername, sshSolrHost, 22);
session.setConfig(config);
session.setPassword(sshSolrPassword);
session.connect();
channel = session.openChannel("exec");
((ChannelExec)channel ).setCommand("cd " + sourcePath);
exec.setInputStream(null);
((ChannelExec)channel ).setErrStream(System.err);
InputStream in = channel .getInputStream();
channel .connect();
int status = checkStatus(channel , in);
channel.disconnect();
...
答案 1 :(得分:0)
问题在于路径。找不到要重命名的文件,如果您使用的是JSCH库,则可以使用 channelSftp.pwd() 知道当前位置。
示例:
Sftp sftp = new Sftp();
sftp.conectar();
ChannelSftp channelSftp = sftp.getChannelSftp();
channelSftp.rename(channelSftp.pwd()+"//"+file,`channelSftp.pwd()`+"//"+"new_dir//"+file);
//or
String url = channelSftp.pwd();
String url_m = channelSftp.pwd()+"/"+directory;
channelSftp.rename(url+file,url_m+file)
答案 2 :(得分:0)
我建议你在inpuStream中读取你的文件,然后使用该函数:
put(InputStream src, String dst)
文档:https://epaul.github.io/jsch-documentation/simple.javadoc/com/jcraft/jsch/ChannelSftp.html
示例:
String fileName = "D:/Folder1/Folder2";
File initialFile = new File(fileName);
InputStream targetStream = new FileInputStream(initialFile);
channelSftp.put(targetStream, "./in/");