在Java中检查服务器上是否存在路径
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21276775/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check whether the path exists on server or not in Java
提问by Ankit Lamba
I am creating a Java program in which I am uploading file to server on a particular path. I am using jSch
for sftp
.
我正在创建一个 Java 程序,在该程序中我将文件上传到特定路径上的服务器。我正在使用jSch
for sftp
。
So, before uploading the file, I want to check if the given directory exists on server or not.
因此,在上传文件之前,我想检查服务器上是否存在给定目录。
if(path exists)
//upload file to the location
else
//create the directory and then upload the file.
How do I check the path exists or not?
如何检查路径是否存在?
Note:I am executing the code on a client that will check for the existence of a remote directory on a server. So please don't suggest
File.exists()
.
注意:我正在客户端上执行代码,该代码将检查服务器上是否存在远程目录。所以请不要建议
File.exists()
。
采纳答案by Brian Roach
Reading the Documentation for ChannelSftpit would appear you can just lstat
the directory:
阅读ChannelSftp的文档,您似乎可以只lstat
使用目录:
SftpATTRS attrs = channelSftp.lstat(path);
If that throws an exception, it doesn't exist. You can then use channelSftp.mkdir(path)
to create it.
如果抛出异常,则它不存在。然后您可以使用channelSftp.mkdir(path)
它来创建它。
回答by Rhand
I am not familiar with this library, but from this example code: http://www.jcraft.com/jsch/examples/Sftp.java.htmlit looks like you can use
我不熟悉这个库,但从这个示例代码:http: //www.jcraft.com/jsch/examples/Sftp.java.html看起来你可以使用
ChannelSftp c = ..;
c.ls('<path>')
to retrieve the file, this should tell you whether it exists or not.
要检索文件,这应该告诉您它是否存在。