【问题标题】:java.io.IOException: inputstream is closedjava.io.IOException:输入流已关闭
【发布时间】:2022-12-12 23:55:43
【问题描述】:

我正在尝试将文件从 SFTP 传输到 SharePoint 文件夹。
我已经使用 JSCH 库连接到 SFTP。

以下是我的代码 sn-p 到上传文件.

private void sharePointUpload(String driveId, String targetLocation, InputStream inputStream, String fileName) throws FileNotFoundException, IOException {
    UploadSession uploadSession = this.graphServiceClient.drives().byId(driveId).root().itemWithPath(targetLocation +"/"+ fileName)
                                    .createUploadSession(this.uploadParams).buildRequest().post();
    byte[] inputStreamByteArray = IOUtils.toByteArray(inputStream);
    LargeFileUploadTask<DriveItem> largeFileUploadTask = new LargeFileUploadTask<DriveItem>
                                        (uploadSession, this.graphServiceClient, new ByteArrayInputStream(inputStreamByteArray), inputStreamByteArray.length, DriveItem.class);
    largeFileUploadTask.upload(MAXIMUM_CHUNK_SIZE);
}

我收到错误java.io.IOException:输入流已关闭IOUtils.toByteArray(inputStream)
根本原因是什么?

错误日志

更新 :
代码片段到获取输入流

for (int i = 0; i < list.size(); i++) {
    sourcefilenamelist = list.get(i);
    logger.log(proccessingFileLogText + sourcefilenamelist);
    if (targetFilenameflag.equalsIgnoreCase("false")) {
        targetFilename = sourcefilenamelist;
    }
    
    InputStream stream = sourceChannelSftp.get(sourceLocation + '/' + sourcefilenamelist);
    InputStreamDetails decryptEncryptInputStreamDetails = decryptEncryptInputStream(stream, targetFilename, sourceEncryption, targetEncryption, jobjsource);
    stream = decryptEncryptInputStreamDetails.getInputStream();
    targetFilename = decryptEncryptInputStreamDetails.getFileName();
    
    try {
        sharePointUpload(targetDriveId, targetLocation, stream, targetFilename);
    } catch (ClientException e) {
        logger.log(" Graph API ClientException : ");
        logger.log( "e.getMessage()     :- " + e.getMessage() );
        logger.log( "RootCauseMessage   :- " + ExceptionUtils.getRootCauseMessage(e) );
//                                      isSQSMessageRetry = true;
        throw e;
    }
...
...
...
}

PS:很少有文件是从List转过来的

【问题讨论】:

  • 根本原因是它说 - sharePointUpload 是用封闭的 inputStream 调用的。您需要检查/提供创建此流的代码的详细信息。
  • @Tintin 很少有文件从列表中传输...我添加了代码来获取文件和方法调用
  • 我们需要minimal reproducible example。您的问题与 SharePoint 有任何关系吗?如果您只是在循环中读取 InputStream 而不使用任何 SharePoint 代码会怎样?
  • @MartinPrikryl 我提到过调用时发生错误,IOUtils.toByteArray(inputStream)分享点上传()... 将文件上传到 SharePoint 时无事可做。
  • 那么为什么要使用 SharePoint 使问题复杂化呢?你为什么不发布一个简单的顺序代码 sn-p 来重现这个问题?而不是使用与问题无关的代码的两个脱节方法?

标签: java sftp inputstream jsch apache-commons-io


【解决方案1】:

当调用该方法时,您将为其提供关闭的 InputStream。确保您提供的 InputStream 已打开并已初始化。

【讨论】:

  • 从 List 传输的文件很少...我添加了代码来获取文件和方法调用
【解决方案2】:

此行为您提供了一个封闭的 InputStream:

InputStream stream = sourceChannelSftp.get(sourceLocation + '/' + sourcefilenamelist);

也许您发送给它的 sourceLocation 参数不正确?

【讨论】:

  • 我提到PS:很少有文件是从List转过来的...所以我不认为这是sourceLocation问题。
  • 正如目前所写,您的答案尚不清楚。请edit 添加更多详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写出好的答案的信息in the help center
【解决方案3】:

假设流有效,但一旦流被读取,它将关闭,尝试检查它是否正在其他地方读取。

【讨论】:

    【解决方案4】:

    我尝试了以上选项,但似乎都不适用于我的用例。

    在我的用例中,我们并行使用 JSCH 将文件上传到 sftp。大多数上传都是成功的,但很少有人因上述错误而失败。

    在调查实现时,我发现库从未关闭的池中返回连接。在我们的例子中,其中一个线程在另一个并发线程完成上传之前关闭了连接,因此导致IOException: input stream is closed error

    我们在 cachingConnectionFactory 上启用了 testSession 特性。它将返回池连接 if it is not closedcan actually list files from the SFTP。这解决了我们的问题。

    在此处发布示例代码

    DefaultSftpSessionFactory sessionFactory1 = new DefaultSftpSessionFactory();
        sessionFactory1.setHost("host");
        sessionFactory1.setPort(21);
        sessionFactory1.setUser("userName");
        sessionFactory1.setPassword("password");
        CachingSessionFactory<ChannelSftp.LsEntry> cachingSessionFactory = new CachingSessionFactory<>(sessionFactory, 10);
        cachingSessionFactory.setSessionWaitTimeout(2000);
        cachingSessionFactory.setTestSession(true);`
     
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 2019-06-09
      • 1970-01-01
      • 1970-01-01
      • 2016-04-05
      • 2016-04-28
      • 1970-01-01
      相关资源
      最近更新 更多