Shantaram Tupe
Shantaram Tupe

Reputation: 1666

java.io.IOException: inputstream is closed

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

enter image description here

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

Upvotes: 2

Views: 7246

Answers (3)

mindlid
mindlid

Reputation: 2676

supposing the stream works, but once the stream is read, it will close, try checking if it's being read somewhere else.

Upvotes: 0

ant1eicher
ant1eicher

Reputation: 11

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?

Upvotes: 0

ItsVaske
ItsVaske

Reputation: 27

You are providing it with closed InputStream when the method is being called. Make sure the InputStream you are providing is open and initialized.

Upvotes: 0

Related Questions