【问题标题】:Code in java to check if a directory exists or not on remote serverjava中的代码检查远程服务器上是否存在目录
【发布时间】:2020-03-22 10:00:24
【问题描述】:

我正在尝试用 java 编写代码来检查远程服务器上是否存在目录。我尝试了Check whether the path exists on server or not in Java 上提到的一些东西。

ChannelSftp channelSftp = new ChannelSftp();
SftpATTRS attrs = null;
try {
    String currentDirectory = channelSftp.pwd();
    System.out.println(currentDirectory);
} catch (SftpException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}
try {
    attrs = channelSftp.stat("/x/web/STAGE2MA49/qatools/capturescripts_new");
} catch (Exception e) {
    // TODO Auto-generated catch block
    System.out.println(" not found");
}

但是上面的代码确实成功了。下面是错误堆栈

at com.jcraft.jsch.ChannelSftp.getHome(ChannelSftp.java:2443)
at com.jcraft.jsch.ChannelSftp.getCwd(ChannelSftp.java:2452)
at com.jcraft.jsch.ChannelSftp.pwd(ChannelSftp.java:2429)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:215)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:142)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:336)
at org.testng.SuiteRunner.run(SuiteRunner.java:268)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1244)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1169)
at org.testng.TestNG.run(TestNG.java:1064)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:113)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:206)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:177)
Caused by: java.lang.NullPointerException
at com.jcraft.jsch.ChannelSftp.getHome(ChannelSftp.java:2435)
... 21 more

【问题讨论】:

  • “但确实成功了。得到空指针异常”应该是“...没有成功了...”。无论如何 NullPointerException 是 very common problem 但没有 minimal reproducible example 我们无法帮助您进行调试。
  • 我的猜测是,由于未找到路径 SftpATTRS attrs = null; 保持为 null 所以后来涉及它的操作,如 attrs.someMethod(); 最终会调用 null.someMethod();null没有任何方法并抛出 NPE。要么执行依赖于 try 部分中的 attrs 值的代码,该部分为它分配非空值,或者每次使用它时检查它是否像 if(attrs != null) 那样不为空,或者不要将其包装在 @987654334 @ 并将您遇到的任何异常重新抛出给执行您的代码的客户,然后他将不得不决定如何处理此类异常。

标签: java ssh server ftp


【解决方案1】:
ChannelSftp channelSftp = new ChannelSftp();

您不能仅仅构造一个ChannelSftp 对象并期望它能够工作。您需要创建一个到远程服务器的 SSH 会话,然后请求一个 SFTP 通道来运行该会话。

question that you linked to 引用了一个 example program,它使用 Jsch 与远程服务器建立 SFTP 连接。我将在这里复制其中的一部分:

JSch jsch=new JSch();
...
Session session=jsch.getSession(user, host, port);
...
session.connect();
...
Channel channel=session.openChannel("sftp");
channel.connect();
ChannelSftp c=(ChannelSftp)channel;

使用 JSCH API,您将创建一个 Jsch 对象,然后使用它创建一个 Session,然后使用 Session 对象连接到远程服务器。假设有效,您可以请求通过会话运行的“sftp”通道。这将返回您需要的 ChannelSftp 对象。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-16
    • 1970-01-01
    • 2012-06-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-26
    相关资源
    最近更新 更多