A.P.S
A.P.S

Reputation: 1164

Reading remote Ubuntu directory using ssh in java?

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.

Upvotes: 1

Views: 3146

Answers (4)

Vladimir Vaschenko
Vladimir Vaschenko

Reputation: 576

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

Upvotes: 0

user2284545
user2284545

Reputation:

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

Upvotes: 0

Menelaos
Menelaos

Reputation: 26549

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

Upvotes: 1

Ahmed
Ahmed

Reputation: 662

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 );
}

Upvotes: 0

Related Questions