简体   繁体   中英

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?

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 .

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

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM