Felix
Felix

Reputation: 1263

how to get remote linux server file inputstream

I am trying to read a file which is in the remote linux server. But I do not know how to get the inputstream of the file using java.

How can this be done?

Upvotes: 0

Views: 5498

Answers (5)

user1516873
user1516873

Reputation: 5183

You can use any ssh java lib, as was mentioned in other answers, or mount directory with file as NFS share folder. After mounting you can use usual java API to acsess file.

Example

Upvotes: 0

phsym
phsym

Reputation: 1384

It depends on how is the file available. Is it by HTTP, FTP, SFTP or through a server you wrote yourself ?

If your want to get the file through HTTP, you can use this :

HttpURLConnection connec = (HttpURLConnection)new URL("http://host/file").openConnection();
if(connec.getResponseCode() != connec.HTTP_OK)
{
    System.err.println("Not OK");
    return;
}
System.out.println("length = " + connec.getContentLength());
System.out.println("Type = " + connec.getContentType());
InputStream in = connec.getInputStream();
//Now you can read the file content with in

There is also Jsch library which is very good for SFTP / SCP

Upvotes: 0

Assuming you have a working connection to the server and access to the file, you can create a File object with the URI of the file:

File f = new File(uri);
FileInputStream fis = new FileInputStream(f);

The URI should be the URI to the file, for example "file://server/path/to/file". See also the Javadoc for File(URI) .

Upvotes: 0

ilvez
ilvez

Reputation: 1285

Maybe SSHJ can help you? https://github.com/shikhar/sshj

Features of the library include:

  • reading known_hosts files for host key verification
  • publickey, password and keyboard-interactive authentication
  • command, subsystem and shell channels
  • local and remote port forwarding
  • scp + complete sftp version 0-3 implementation

Upvotes: 0

Giovanni Caporaletti
Giovanni Caporaletti

Reputation: 5546

Assuming that by "remote linux server" you mean "remote linux shell", you should use an ssh library like JSch. You can find a file download example here.

Upvotes: 2

Related Questions