I'm creating a Java program in which I upload a file to a 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?
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()
.
Reading the Documentation for ChannelSftp it would appear you can just lstat
the directory:
SftpATTRS attrs = channelSftp.lstat(path);
If that throws an exception, it doesn't exist. You can then use channelSftp.mkdir(path)
to create it.