从S3获取数据在html表示,为什么上传到S3的文件具有内容类型application / octet-stream,除非我将文件命名为.html...

因为你必须在发送之前在结尾设置内容类型,使用putObject方法;

ObjectMetadata md = new ObjectMetadata();

InputStream myInputStream = new ByteArrayInputStream(bFile);

md.setContentLength(bFile.length);

md.setContentType("text/html");

md.setContentEncoding("UTF-8");

s3client.putObject(new PutObjectRequest(bucketName, keyName, myInputStream, md));上传后,内容类型设置为“text / html”

WRi2z.png

这是一个工作的虚拟代码,检查出来,我刚试过,它正在工作;

public class TestAWS {

//TEST

private static String bucketName = "whateverBucket";

public static void main(String[] args) throws Exception {

BasicAWSCredentials awsCreds = new BasicAWSCredentials("whatever", "whatever");

AmazonS3 s3client = new AmazonS3Client(awsCreds);

try

{

String uploadFileName = "D:\\try.txt";

String keyName = "newFile.txt";

System.out.println("Uploading a new object to S3 from a file\n");

File file = new File(uploadFileName);

//bFile will be the placeholder of file bytes

byte[] bFile = new byte[(int) file.length()];

FileInputStream fileInputStream=null;

//convert file into array of bytes

fileInputStream = new FileInputStream(file);

fileInputStream.read(bFile);

fileInputStream.close();

ObjectMetadata md = new ObjectMetadata();

InputStream myInputStream = new ByteArrayInputStream(bFile);

md.setContentLength(bFile.length);

md.setContentType("text/html");

md.setContentEncoding("UTF-8");

s3client.putObject(new PutObjectRequest(bucketName, keyName, myInputStream, md));

} catch (AmazonServiceException ase)

{

System.out.println("Caught an AmazonServiceException, which "

+ "means your request made it "

+ "to Amazon S3, but was rejected with an error response"

+ " for some reason.");

System.out.println("Error Message: " + ase.getMessage());

System.out.println("HTTP Status Code: " + ase.getStatusCode());

System.out.println("AWS Error Code: " + ase.getErrorCode());

System.out.println("Error Type: " + ase.getErrorType());

System.out.println("Request ID: " + ase.getRequestId());

} catch (AmazonClientException ace)

{

System.out.println("Caught an AmazonClientException, which "

+ "means the client encountered "

+ "an internal error while trying to "

+ "communicate with S3, "

+ "such as not being able to access the network.");

System.out.println("Error Message: " + ace.getMessage());

}

}

}希望它有所帮助。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值