1

I want to read the content of a remote directory using java.

The directory is on a machine running Ubuntu. Right clicking on the folder should give the share folder option and its installed samba client for windows sharing, but I don't have any machine running Windows.

I'm looking for a java api library to access the remote directory content?

User will only provide username, password, ip and folder name.

eg [//172.17.0.1/sharefolder/demo/]

Thanks.

A.P.S
  • 1,124
  • 4
  • 17
  • 36
  • I think here it needs to be specified whether it is for SFTP or SAMBA within the title and question body. – Menelaos Apr 08 '13 at 21:19

4 Answers4

1

For a Samba Share: Even SAMBA shares in linux use the same protocol as windows shares. So the post here can help: How can I mount a windows drive in Java? Basically, you could mount the shared location as a network drive using "net use" command . You could call this either through windows console, or through a java Process.

For a SFTP location:

If you don't have a problem with calling/using an external command you could use sshfs (either out of java or through Process) to mount the remote directory into a local folder.

See: http://numberformat.wordpress.com/2010/03/01/how-to-mount-a-remote-ssh-filesystem-using-sshfs/

If you want pure java on how to access SFTP,I read that a library called JSch can be used to access SFTP directly from java. See:

If it's another type please specify

Community
  • 1
  • 1
Menelaos
  • 23,508
  • 18
  • 90
  • 155
  • Thanks for useful link. But i'm looking for JAVA based API to pass user and system info, then access the directory without mounting. I have founded few APi such as sshj , JCFS etc. But all of them are using secure FTP [get or put] for files. – A.P.S Apr 08 '13 at 11:47
  • Hi, thanks! Question, the protocol your using is SFTP, Samba, or another one? – Menelaos Apr 08 '13 at 11:49
  • SFTP, can we traverse all the file and folders ? Check, whether it is directory or not? – A.P.S Apr 08 '13 at 12:54
  • If you mount it within a folder then it will look like as if it is part of the file system, so you will be able to traverse. Java sees what the operating systems tells it. – Menelaos Apr 08 '13 at 13:25
  • Is there any code to mount the remote directory using ssh and java programming interface? – A.P.S Apr 08 '13 at 13:54
  • You could call sshfs using Runtime.getRuntime().exec and spawn a new process to do the mount. If you want pure java on how to access SFTP,I read that a library called JSch can be used to access SFTP directly from java. See: http://chrisjordan.ca/post/15052396308/java-sftp http://www.jcraft.com/jsch/examples/Sftp.java.html – Menelaos Apr 08 '13 at 16:45
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/27784/discussion-between-menelaos-and-a-p-s) – Menelaos Apr 08 '13 at 16:48
0

You might find the The Java CIFS Client Library having the API you need - it is useful for both server and client.

Here is an example taken from their documentation to retrieve a file:

import jcifs.smb.*;

jcifs.Config.setProperty( "jcifs.netbios.wins", "192.168.1.220" );
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domain", "username", "password");
SmbFileInputStream in = new SmbFileInputStream("smb://host/c/My Documents/somefile.txt", auth);
byte[] b = new byte[8192];
int n;
while(( n = in.read( b )) > 0 ) {
    System.out.write( b, 0, n );
}
Ahmed
  • 590
  • 8
  • 19
  • Does JCIFS lib allow to read directory content, even if entire cluster is Ubuntu machine only? Secondly can we use ssh to do the same process in java? – A.P.S Apr 08 '13 at 11:05
  • It appears that you are wanting an sftp-based API not SMB/CIFS. You should edit the question to reflect that accordingly. At any rate, hopefully the answers that Menelaos will help you. Good luck! – Ahmed Apr 08 '13 at 17:27
0

For SFTP consider using JSCAPE's Secure FTP Factory. Documentation with code examples can be found here.

0

jsch-nio is a fully functional unix/linux java FileSystemProvider over ssh.