在Java中检查路径是否存在于服务器上 [英] Check whether the path exists on server or not in Java

查看:143
本文介绍了在Java中检查路径是否存在于服务器上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Java程序,其中将文件上传到特定路径上的服务器.我正在将jSch用于sftp.

I am creating a Java program in which I am uploading file to server on a particular path. I am using 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?

注意::我正在客户端上执行代码,该代码将检查服务器上是否存在远程目录.因此,请不要建议File.exists().

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().

推荐答案

阅读 ChannelSftp的文档,您似乎可以仅lstat目录:

Reading the Documentation for ChannelSftp it would appear you can just lstat the directory:

SftpATTRS attrs = channelSftp.lstat(path);

如果引发异常,则该异常不存在.然后,您可以使用channelSftp.mkdir(path)进行创建.

If that throws an exception, it doesn't exist. You can then use channelSftp.mkdir(path) to create it.

这篇关于在Java中检查路径是否存在于服务器上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆