当我尝试将对象添加到s3时,它给出AmazonClientException: Data read has a different length than the expected
。
但是问题是预期的长度显示比实际大小大32位。
例如:5076681字节是磁盘上的实际大小。但是我们得到一个例外,说预期为5076713。
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
InputStream in = connection.getInputStream();
ObjectMetadata metadata = new ObjectMetadata();
//adding metadata content length along with request
metadata.setContentLength(contentLengthParam);
PutObjectRequest putObjectRequest = new PutObjectRequest("Bucket", "fileName", in, metadata)
.withCannedAcl(CannedAccessControlList.PublicRead);
PutObjectResult result = s3AwsClient.putObject(putObjectRequest);
我得到的例外:
com.amazonaws.AmazonClientException: More data read than expected: dataLength=5076713; expectedLength=5076681; includeSkipped=false; in.getClass()=class com.amazonaws.internal.ReleasableInputStream; markedSupported=false; marked=0; resetSinceLastMarked=false; markCount=0; resetCount=0
com.amazonaws.util.LengthCheckInputStream.checkLength(LengthCheckInputStream.java:156)
com.amazonaws.util.LengthCheckInputStream.read(LengthCheckInputStream.java:110)
com.amazonaws.internal.SdkFilterInputStream.read(SdkFilterInputStream.java:72)
com.amazonaws.services.s3.internal.MD5DigestCalculatingInputStream.read(MD5DigestCalculatingInputStream.java:98)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
这仅表示HttpURLConnection
的输入流中的数据多于contentLengthParam
。您可以做的是将连接的输入流中的所有内容读入缓冲区,计算其长度,然后将该缓冲区写入S3。