【发布时间】: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