File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Distributed Java and the fly likes Reading file from remote server using SFTP connection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Distributed Java
Bookmark "Reading file from remote server using SFTP connection" Watch "Reading file from remote server using SFTP connection" New topic

Reading file from remote server using SFTP connection

Srikanth Madasu
Ranch Hand

Joined: Sep 10, 2008
Posts: 48

I have the following requirement :

I have to read a log file on a remote server and based on the error code I need to download some of the files on it to my application server. I have written code for downloading/uploading files and its working fine. But I am struck on reading the file. I dont wanna download the log file, I wanna open it and read the error codes from it. I dont find any correct method for doing this

This is the code I am using for downloading files..

But I dont wanna download that file, but I wanna open and read its contents.

hmm its 4:30 in the evening and I am out of ideas, and I posted it here ..

Any help is appreciated...

Thanks in advance

If it's easy.. then everybody would do it!
Srikanth Madasu
Ranch Hand

Joined: Sep 10, 2008
Posts: 48

My other colleagues at work did a research and told that it is not possible to read a file from a remote server. the only way is to download it first to local file system and then read it.

So we changed our requirements.

Thanks if anyone have spend time on this
purushotham mekalacheruvu

Joined: Apr 19, 2013
Posts: 1
Hi EveryOne, I saw some of the posts as ,to read a file from Remote Server they are telling first we have to download file to local and read that file.
from my point of view no need to download file. We can open an Inputstream on that file and read the content. below is the example.

String hostname = "";
String username = "";
String password = "";
//Connect to SFTP server location as below
SshParameters params = new SshParameters(hostname,username,password);
Sftp sftp = new Sftp(params);
//Once connected to server get InputStream as below

InputStream io = sftp.getInputStream(fileName,0L)// File name i.e to be read and second argument is offset
DataInputStream in = new DataInputStream(io);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
//Read line by line as below
String record="";
while((record=br.readLine()) != null){
// What ever you required

Please let me know if you have any questions..
Mohamed Iqzas
Ranch Hand

Joined: Jan 02, 2012
Posts: 72

I am interested to implement a similar functionality.

Please let me know in which package the following classes are present:


Eclipse is not suggesting any imports for these. Any external jars need to be added?

ssh is a part of linux commands or its a common way to connect to a remote server? am using only win xp. Thanks.
Richard Tookey

Joined: Aug 27, 2012
Posts: 1166
Jignesh Thakkar

Joined: Oct 09, 2013
Posts: 1
Refer this link :
I agree. Here's the link:
subject: Reading file from remote server using SFTP connection
It's not a secret anymore!