File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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
Author

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
Srik



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
Greenhorn

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

Joined: Jan 02, 2012
Posts: 53

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
Ranch Hand

Joined: Aug 27, 2012
Posts: 960
    
  10

http://www.jcraft.com/jsch/
Jignesh Thakkar
Greenhorn

Joined: Oct 09, 2013
Posts: 1
Refer this link : http://mylrsolutions.blogspot.in/2013/10/sftp-connection-using-java-for-remote.html
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading file from remote server using SFTP connection
 
Similar Threads
write files
How do I read a file saved on a SFTP server using jScape's SFTP Api
Downloading a file from web site
problem retrieving a zip file from FTP server using org.apache.commons.net.ftp.
Unable to connect to Windows Machine Using FTPClient...