• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Reading file from remote server using SFTP connection

 
Ranch Hand
Posts: 50
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 50
Hibernate Eclipse IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 77
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 1166
17
Netbeans IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.jcraft.com/jsch/
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Refer this link : http://mylrsolutions.blogspot.in/2013/10/sftp-connection-using-java-for-remote.html
 
Did you just should on me? You should read this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic