Reputation: 1263
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
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.
Upvotes: 0
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
Reputation: 8245
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
Reputation: 1285
Maybe SSHJ can help you? https://github.com/shikhar/sshj
Features of the library include:
Upvotes: 0
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