IOUtils.closeQuietly 似乎已被弃用,取而代之的是 TryWithResources,但是当我需要在异步事件侦听器中关闭资源时,它如何帮助我,比如当我使用 AmazonS3 和 TransferManager 上传文件时?
final String key = rs.getString("id");
final InputStream data = rs.getBinaryStream("data");
final long length = rs.getLong("length");
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentLength(length);
Upload upload = transferManager.upload(s3bucketName, key, data, objectMetadata);
upload.addProgressListener((com.amazonaws.event.ProgressListener) p -> {
switch (p.getEventType()) {
case TRANSFER_COMPLETED_EVENT:
case TRANSFER_FAILED_EVENT:
case TRANSFER_CANCELED_EVENT:
IOUtils.closeQuietly(data);
default:
break;
}
});