java – 为什么上传到S3的文件具有内容类型application / octet-stream,除非我命名文件.html

前端之家收集整理的这篇文章主要介绍了java – 为什么上传到S3的文件具有内容类型application / octet-stream,除非我命名文件.html前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
即使我将内容类型设置为text / html,它在S3上最终作为应用程序/八位字节流.
ByteArrayInputStream contentsAsStream = new ByteArrayInputStream(contentAsBytes);
ObjectMetadata md = new ObjectMetadata();
md.setContentLength(contentAsBytes.length);
md.setContentType("text/html");
s3.putObject(new PutObjectRequest(ARTIST_BUCKET_NAME,artistId,contentsAsStream,md));

然而,如果我命名该文件,以最终为.html

s3.putObject(new PutObjectRequest(ARTIST_BUCKET_NAME,artistId + ".html",md));

那么它工作.

我的md对象是否被忽略?如何随着时间的推移,我需要上传数千个文件,所以不能进入S3 UI并手动修复contentType.

解决方法

您必须在代码中执行其他操作.我刚刚尝试使用1.9.6 S3 SDK的代码示例,该文件获取“text / html”内容类型.

以下是确切的(Groovy)代码

class S3Test {
    static void main(String[] args) {

        def s3 = new AmazonS3Client()

        def random = new Random()
        def bucketName = "raniz-playground"
        def keyName = "content-type-test"

        byte[] contentAsBytes = new byte[1024]
        random.nextBytes(contentAsBytes)

        ByteArrayInputStream contentsAsStream = new ByteArrayInputStream(contentAsBytes);
        ObjectMetadata md = new ObjectMetadata();
        md.setContentLength(contentAsBytes.length);
        md.setContentType("text/html");
        s3.putObject(new PutObjectRequest(bucketName,keyName,md))

        def object = s3.getObject(bucketName,keyName)
        println(object.objectMetadata.contentType)
        object.close()
    }
}

程序打印

text/html

而S3元数据也是一样的:

以下是通过网络发送的通信(由Apache HTTP Commons调试日志提供):

>> PUT /content-type-test HTTP/1.1
>> Host: raniz-playground.s3.amazonaws.com
>> Authorization: AWS <nope>
>> User-Agent: aws-sdk-java/1.9.6 Linux/3.2.0-84-generic Java_HotSpot(TM)_64-Bit_Server_VM/25.45-b02/1.8.0_45
>> Date: Fri,12 Jun 2015 02:11:16 GMT
>> Content-Type: text/html
>> Content-Length: 1024
>> Connection: Keep-Alive
>> Expect: 100-continue
<< HTTP/1.1 200 OK
<< x-amz-id-2: mOsmhYGkW+SxipF6S2+CnmiqOhwJ62WfWUkmZk4zU3rzkWCEH9P/bT1hUz27apmO
<< x-amz-request-id: 8706AE3BE8597644
<< Date: Fri,12 Jun 2015 02:11:23 GMT
<< ETag: "6c53debeb28f1d12f7ad388b27c9036d"
<< Content-Length: 0
<< Server: AmazonS3

>> GET /content-type-test HTTP/1.1
>> Host: raniz-playground.s3.amazonaws.com
>> Authorization: AWS <nope>
>> User-Agent: aws-sdk-java/1.9.6 Linux/3.2.0-84-generic Java_HotSpot(TM)_64-Bit_Server_VM/25.45-b02/1.8.0_45
>> Date: Fri,12 Jun 2015 02:11:23 GMT
>> Content-Type: application/x-www-form-urlencoded; charset=utf-8
>> Connection: Keep-Alive
<< HTTP/1.1 200 OK
<< x-amz-id-2: 9U1CQ8yIYBKYyadKi4syaAsr+7BV76Q+5UAGj2w1zDiPC2qZN0NzUCQNv6pWGu7n
<< x-amz-request-id: 6777433366DB6436
<< Date: Fri,12 Jun 2015 02:11:24 GMT
<< Last-Modified: Fri,12 Jun 2015 02:11:23 GMT
<< ETag: "6c53debeb28f1d12f7ad388b27c9036d"
<< Accept-Ranges: bytes
<< Content-Type: text/html
<< Content-Length: 1024
<< Server: AmazonS3

这也是source code显示我们的行为 – 如果您设置内容类型,SDK将不会覆盖它.

原文链接:https://www.f2er.com/java/122032.html

猜你在找的Java相关文章