0

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?

5 Answers 5

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

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

1 Comment

I don`t know what uri it should be. Is it the path of the file?exmaple:"/root/files/chat.log" ?
0

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.