Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading file from remote server using SFTP connection

 
Srikanth Madasu
Ranch Hand
Posts: 48
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Srik


 
Srikanth Madasu
Ranch Hand
Posts: 48
Eclipse IDE Hibernate Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
sftp.connect();
//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..
mailId:purushotham635@gmail.com
 
Mohamed Iqzas
Ranch Hand
Posts: 73
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am interested to implement a similar functionality.

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

ChannelSftp
SshParameters
Sftp

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
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.jcraft.com/jsch/
 
Jignesh Thakkar
Greenhorn
Posts: 2
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic