我使用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()”的方法和多余的前导/尾随“/”的方面做了奇怪的事情,但我不知道甚至在连接后告诉我主目录是否正确。
有什么想法吗?
答案 0 :(得分:1)
感谢大家的意见。
操作系统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
再次非常感谢。
答案 1 :(得分:0)