3

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

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
earthmover
  • 4,395
  • 10
  • 43
  • 74
  • new File("").exists(); – Sudhanshu Umalkar Jan 22 '14 at 07:35
  • @BrianRoach - OP wants to check existence of a file _on a server_ from a client program through ftp. `File.exists()` doesn't do the job. – Ted Hopp Jan 22 '14 at 07:36
  • It would appear that's the case, but honestly, its easy to read it incorrectly the way it's written. I still had to read it twice to see what you meant; I kept ignoring the first sentence. – Brian Roach Jan 22 '14 at 07:40
  • I don't know why the question is downvoted??? I want to check the path on SERVER not on the local machine... File.exists() doesn't work on the server... – earthmover Jan 22 '14 at 07:42
  • You have a server component written in java to check the existence of path or you want client to check it ? – Aman Arora Jan 22 '14 at 07:42
  • What library are you using to upload the file? Doesn't it provide any way to check if the path exist? – Sudhanshu Umalkar Jan 22 '14 at 07:44
  • 2
    I don't know why the downvote, but I can guess: the way your question is worded suggests that a simple call to `File.exists()` or `File.isDirectory()` will do what you want. You should reword it to clarify that you want to emphasize that you need to execute code on a client that will check for the existence of a remote directory on a server. – Ted Hopp Jan 22 '14 at 07:45
  • @Sudhanshu - OP said the jSch library. – Ted Hopp Jan 22 '14 at 07:46

2 Answers2

11

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.

Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • nice try but it doesn't return null if the path doesn't exists, but It throw an exception (no such file). – earthmover Jan 22 '14 at 08:05
  • @AnkitLamba So ... there's your answer. The documentation is so terrible for the library I really couldn't tell, and a quick reading of his code didn't help (it's not exactly self documenting). (edited to change what result is) – Brian Roach Jan 22 '14 at 08:07
  • 1
    @AnkitLamba - You can simply catch the exception to detect that the directory doesn't exist. – Ted Hopp Jan 22 '14 at 08:09
  • You may try to list the contents of parent directory using channelSftp.ls("") and check if the desired folder exist. – Sudhanshu Umalkar Jan 22 '14 at 08:36
  • 2
    @Sudhanshu Why? So you can add unnecessary string processing to your code for fun? – Brian Roach Jan 22 '14 at 08:37
0

I am not familiar with this library, but from this example code: http://www.jcraft.com/jsch/examples/Sftp.java.html it looks like you can use

ChannelSftp c = ..;
c.ls('<path>')

to retrieve the file, this should tell you whether it exists or not.

Rhand
  • 901
  • 7
  • 20