[英]JSch - Server “home” path is always “/” with FreeSSHd and can't change directory
我使用FreeSSHd设置了服务器,并能够使用Putty,包括更改目录和列出文件。 我在主目录中有一些示例.txt文件和一个文件夹。 我使用FreeSSHd将服务器上的主目录设置为“ C:\\ SFTP”(与定义目录为“ $ HOME \\”的HOME变量相反)。
显然,当使用JSch时,
JSch jsch = new JSch();
session = jsch.getSession(username,host,port);
jsch.addIdentity(key.getAbsolutePath());
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setUserInfo(new MyUserInfo());
session.connect();
channel = session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp)channel;
System.out.println("Home: "+channelSftp.getHome());
最后一行仅打印“ Home:/”。 任何尝试(在上述代码之后立即进行)的使用
channelSftp.cd(WORKINGDIR);
结果是
2: No such file
at com.jcraft.jsch.ChannelSftp.throwStatusError(ChannelSftp.java:2833)
at com.jcraft.jsch.ChannelSftp._stat(ChannelSftp.java:2185)
at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:1295)
at com.jcraft.jsch.ChannelSftp.get(ChannelSftp.java:1267)
at test.SFTPTest.main(SFTPTest.java:71)
我认为,如果我扎根了为什么JSch没有正确的主路径(或任何其他路径),这将起作用。 另外,奇怪的是,我使用put()和get()可以毫无问题地上传和下载文件。
我听到过各种各样的事情,人们会深入研究源代码,发现它在解析路径和使用“ _realPath()”方法以及多余的前导/跟踪“ /”方法时会产生奇怪的事情,但我什至没有告诉我连接后主目录正确。
有任何想法吗?
谢谢大家的评论。
在Windows XP操作系统上,我安装了FreeSSHd并设置了默认目录,然后,当我尝试通过控制台ssh进行连接时,该目录为“ /”,我编写了chdir,但目录为:C:\\ Windows \\ system32 \\。 。
我的Java代码:
public void recursiveFolderUpload(String sourcePath, String destinationPath) throws FileNotFoundException {
if (c == null || session == null || !session.isConnected() || !c.isConnected()) {
log.debug("Connection to server is closed. Open it first.");
}
try {
// c.put(sourceFile, destinationFile);
// log.info("Upload successfull.");
File sourceFile = new File(sourcePath);
if (sourceFile.isFile()) {
// copy if it is a file
c.cd(destinationPath);
if (!sourceFile.getName().endsWith("."))
c.put(new FileInputStream(sourceFile), sourceFile.getName(), c.OVERWRITE);
} else {
log.info("Inside else " + sourceFile.getName());
File[] files = sourceFile.listFiles();
if (files != null && !sourceFile.getName().startsWith(".")) {
log.info("Directory remote server: " + c.pwd());
c.cd(destinationPath);
SftpATTRS attrs = null;
// check if the directory is already existing
try {
attrs = c.stat(destinationPath + sourceFile.getName());
} catch (Exception e) {
log.warn(destinationPath + sourceFile.getName() + " not found");
//e.printStackTrace();
}
// else create a directory
if (attrs != null) {
log.info("Directory exists IsDir : " + attrs.isDir());
} else {
log.info("Creating dir /" + sourceFile.getName());
c.mkdir(sourceFile.getName());
}
for (File f : files) {
if(!f.getName().contains(".dtd")){
log.info("Uploading file: " + f.getAbsoluteFile());
recursiveFolderUpload(f.getAbsolutePath(), destinationPath + sourceFile.getName() + "/");
}
}
}
}
} catch (SftpException e) {
e.printStackTrace();
}
}
我的解决方案是只将“ /”输入称为recursiveFolderUpload的方法的输入参数destinationPath
换句话说,我的属性文件比:
properties.host = IP
properties.user = user
properties.pass = pass
properties.port = port
properties.dir = / ---> This points to the directory configured by default in opensshd within windows
再次非常感谢。
SFTP目录应该是当前用户的目录,不一定与SFTP服务的目录相同,我也遇到了同样的问题,因为我与该目录和用户的默认目录设置有关。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.