Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

I am trying to transfer file from SFTP to SharePoint folder.
I have used JSCH Library to connect to SFTP.

Following is my code snippet to upload file .

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

I am getting error java.io.IOException: inputstream is closed at IOUtils.toByteArray(inputStream)
What will be the root cause ?

Error Logs

Update :
Code Snippet to Get 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: Few files are transferred from List

Root cause is that it says - sharePointUpload was called with a closed inputStream. You need to check/provide details of the code where this stream was created. – Tintin Jul 14, 2022 at 20:05 @Tintin Few files were transferred from List... I have added code to get file and method call – Shantaram Tupe Jul 15, 2022 at 5:36 We need minimal reproducible example. Have your question anything to do with SharePoint at all? What if you just read the InputStream in a loop without using any SharePoint code. – Martin Prikryl Jul 15, 2022 at 12:44 @MartinPrikryl I have mentioned error occurred while calling, IOUtils.toByteArray(inputStream) which is in sharePointUplod()... nothing to do while uploading file to SharePoint. – Shantaram Tupe Jul 15, 2022 at 13:30 So why do you complicate your question with SharePoint? Why don't you post a simple sequential code snippet that reproduces the problem? Instead of two disjoined methods with code that it not relevant to the problem? – Martin Prikryl Jul 15, 2022 at 13:41

This line is giving you a closed InputStream:

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

Perhaps the sourceLocation argument that you are sending to it is not correct?

I mentioned PS: Few files are transferred from List... so I don't thing it is sourceLocation issue. – Shantaram Tupe Jul 15, 2022 at 13:31 As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. – Community Jul 17, 2022 at 16:56

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.