I am using JSch to open a SFTP channel to a remote server. I use the below code to open the connection and download the file:
public org.springframework.core.io.Resource download(){
JSch jsch = new Jsch();
Session session = jsch.get("root", "192.168.1.10", 22);
session.setPassword("root");
session.setConfig("StrictHostKeyChecking","no");
session.connect();
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
InputStream is = channelSftp.get("/root/example.mp4");
channelSftp.exit();
session.disconnect();
return new org.springframework.core.io.InputStreamResource(is);
}
The problem is:
- If I use
exit()
and/ordisconnect()
method, there will bePipe closed
exception thrown - If I don't, my method return a
Resource
successfully but the channel/session is still inconnected
state.
So I have a question for this implementation whether there is something wrong ? If there isn't, will the number of sessions increase till the SFTP server denies or they will be closed at a time in future, how can I handle this ?
Thank you in advanced