【问题标题】:Use Jsch with InputStream is throwing NullPointerException [duplicate]将 Jsch 与 InputStream 一起使用会引发 NullPointerException [重复]
【发布时间】:2021-06-04 10:31:13
【问题描述】:

我正在尝试通过 SFTP 协议使用 JSCH 发送文件。

这是FileService 文件

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

但是 JSch 正在抛出 NullPointException 并带有以下消息: java.lang.NullPointerException: Cannot invoke "com.jcraft.jsch.Channel$MyPipedInputStream.updateReadSide()" because "this.io_in" is null

我已经尝试过OutputStream,结果是一样的。

你能帮帮我吗?

[更新 1]

尝试@Ogod 建议后

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

它抛出了以下消息java.io.IOException: channel is broken

【问题讨论】:

  • @user207421 原来的错误是NullPointerException。在评论中(现在已删除 - 为什么?)我建议在 channel.put(...) 之前尝试 channel.start()。因为这个建议并不完全正确,下一个错误是IOException
  • channel.start() 只是朝着正确方向迈出的一步,但不应在外部调用此方法。正确的方法是channel.connect(),我的答案中写下了。无论如何,应该删除问题中的块 [Update 1],因为如果没有不再可用的 cmets,它会令人困惑。

标签: java jsch


【解决方案1】:

您只是在执行channel.put(...) 之前错过了对channel.connect() 的调用。这将设置channel,以便将内部变量io_in 分配给InputStream(这修复了NullPointerException)。

此外,如果您不再需要 channel,则应通过调用 channel.disconnect()channel.exit() 正确关闭它。这将确保所有资源都被释放。

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.connect();   // this should do the trick
            channel.put(is, "test.txt");
            channel.exit();      // don't forget this
        } 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");
    }
}

【讨论】:

  • 如果您用文字而不是代码来解释问题是什么以及您如何解决它,这将是一个更好的答案。
  • @VGR 感谢您的提示,我在我的答案中添加了一个解释,希望能澄清错误的根源。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-01
  • 2018-03-20
  • 2021-06-07
  • 2018-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多