Reading file as inputstream from an SFTP server | Community
Skip to main content
Level 2
May 7, 2024
Solved

Reading file as inputstream from an SFTP server

  • May 7, 2024
  • 1 reply
  • 2695 views

Hi Team

 

I am using jsch to fetch a file from SFTP server using JSCH using the below snippet.

the connect is getting succeeded, however the inputstream is not being able to fetched with the file path.

could anybody help me with this.

 

if anybody has any working snippet, please let me know.

 

 Session session = jsch.getSession(user, host, 22);;
        session.setConfig("StrictHostKeyChecking", "no");
        session.setPassword(password);
        session.connect();
        logger.info("session connected"+session.isConnected());
        Channel channel = session.openChannel("sftp");
        channel.connect();
                logger.info("channel connected"+channel.isConnected());
        ChannelSftp channelSftp = (ChannelSftp)channel;
 
        //ChannelSftp sftp = (ChannelSftp) session.openChannel("sftp");
        InputStream stream = channelSftp.get("/faresheet/abc.pdf");
        logger.info("stream connected"+stream.available());
        BufferedReader br = new BufferedReader(new InputStreamReader(stream));

 

Regards

Bishnu

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by EstebanBustamante

Hi, 

This is not a question directly related to AEM, but rather about pure Java code. Therefore, you could also search for an answer outside of this community. Additionally, I suggest double-checking basic aspects such as whether the file exists, if the path is correct, if any exceptions are being thrown, and if you have proper rights.

You could refactor and ensure that no exceptions occur, something like this

Session session = jsch.getSession(user, host, 22); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword(password); session.connect(); logger.info("session connected: " + session.isConnected()); Channel channel = session.openChannel("sftp"); channel.connect(); logger.info("channel connected: " + channel.isConnected()); ChannelSftp channelSftp = (ChannelSftp) channel; try { InputStream stream = channelSftp.get("/faresheet/abc.pdf"); logger.info("File fetched successfully"); // Process the input stream here... // Close the stream and disconnect the channel stream.close(); channel.disconnect(); session.disconnect(); } catch (SftpException | IOException e) { logger.error("Error fetching file: " + e.getMessage()); }


Also, you could use the method ChannelSftp.ls() to check if the file is accessible. Please look at this thread for more insights: https://stackoverflow.com/questions/35358762/list-all-files-in-remote-server-using-jsch 

 

Hope this helps

1 reply

EstebanBustamante
Community Advisor and Adobe Champion
EstebanBustamanteCommunity Advisor and Adobe ChampionAccepted solution
Community Advisor and Adobe Champion
May 7, 2024

Hi, 

This is not a question directly related to AEM, but rather about pure Java code. Therefore, you could also search for an answer outside of this community. Additionally, I suggest double-checking basic aspects such as whether the file exists, if the path is correct, if any exceptions are being thrown, and if you have proper rights.

You could refactor and ensure that no exceptions occur, something like this

Session session = jsch.getSession(user, host, 22); session.setConfig("StrictHostKeyChecking", "no"); session.setPassword(password); session.connect(); logger.info("session connected: " + session.isConnected()); Channel channel = session.openChannel("sftp"); channel.connect(); logger.info("channel connected: " + channel.isConnected()); ChannelSftp channelSftp = (ChannelSftp) channel; try { InputStream stream = channelSftp.get("/faresheet/abc.pdf"); logger.info("File fetched successfully"); // Process the input stream here... // Close the stream and disconnect the channel stream.close(); channel.disconnect(); session.disconnect(); } catch (SftpException | IOException e) { logger.error("Error fetching file: " + e.getMessage()); }


Also, you could use the method ChannelSftp.ls() to check if the file is accessible. Please look at this thread for more insights: https://stackoverflow.com/questions/35358762/list-all-files-in-remote-server-using-jsch 

 

Hope this helps

Esteban Bustamante