该异常表示下载的文件大小与目标大小不匹配。解决方法可以尝试以下两种方式:
try { S3Object obj = client.getObject(new GetObjectRequest(bucketName, key)); InputStream input = obj.getObjectContent(); FileOutputStream os = new FileOutputStream(new File(localFilePath));
byte[] read_buf = new byte[1024];
int read_len;
while ((read_len = input.read(read_buf)) > 0) {
os.write(read_buf, 0, read_len);
}
input.close();
os.close();
} catch (AmazonServiceException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }
TransferManager xfer_mgr = new TransferManager(client); try { Download xfer = xfer_mgr.download(bucketName, key, new File(localFilePath)); xfer.waitForCompletion(); } catch (AmazonServiceException e) { System.err.println(e.getErrorMessage()); System.exit(1); } finally { xfer_mgr.shutdownNow(false); }