I'm trying to send a file using JSCH over SFTP protocol.
Here is the FileService
file
public class FileService {
public void send(){
String str = "this is a test";
InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
try {
var channel = setupJsch();
channel.put(is, "test.txt");
} catch (JSchException | SftpException e) {
e.printStackTrace();
}
}
private ChannelSftp setupJsch() throws JSchException {
JSch jsch = new JSch();
Session jschSession = jsch.getSession("foo", "localhost", 2222);
jschSession.setPassword("pass");
jschSession.setConfig("StrictHostKeyChecking", "no");
jschSession.connect();
return (ChannelSftp) jschSession.openChannel("sftp");
}
}
But the JSch
is throwing a NullPointException
with this message:
java.lang.NullPointerException: Cannot invoke "com.jcraft.jsch.Channel$MyPipedInputStream.updateReadSide()" because "this.io_in" is null
I already tried with OutputStream
and the result is the same.
Could you help me?
[Update 1]
After trying the @Ogod suggestion
public void send(){
String str = "this is a test";
InputStream is = new ByteArrayInputStream(str.getBytes(StandardCharsets.UTF_8));
try {
var channel = setupJsch();
channel.start();
channel.put(is, "test.txt");
} catch (JSchException | SftpException e) {
e.printStackTrace();
}
}
It has thrown the following message java.io.IOException: channel is broken