gpt4 book ai didi

java - Spring AWS S3 : How to upload an image and get a URL which shows it rather than downloads it?

转载 作者:搜寻专家 更新时间:2023-11-01 03:02:32 26 4
gpt4 key购买 nike

所以我的问题是,当我打开上传文件的 URL 时,它会下载它。我做了一些研究,问题是我应该以某种方式向 S3 指定它是一个图像。目前这是处理上传的功能,但我不知道如何在那里设置类型。或者是否有识别图像的内置功能,当打开 URL 时它会自动显示它们而不是下载它们?

谢谢

public void writeResource(byte[] bytes, String fileName) throws IOException
{
Resource resource = this.resourceLoader.getResource("s3://bucket/" + fileName);
WritableResource writableResource = (WritableResource) resource;
try (OutputStream outputStream = writableResource.getOutputStream())
{
outputStream.write(bytes);
}
}

最佳答案

 @Autowired
private AmazonS3 amazonS3;

ObjectMetadata meta = new ObjectMetadata();
meta.setContentType(file.getContentType());
meta.setContentLength(file.getSize());
meta.setHeader("filename", fileName);

ByteArrayInputStream bis = new ByteArrayInputStream(file.getBytes());

TransferManager transferManager = new TransferManager(this.amazonS3);
transferManager.upload(bucket, filename, bis, meta);

我就是这样解决的。希望这对某人有帮助。 :)

关于java - Spring AWS S3 : How to upload an image and get a URL which shows it rather than downloads it?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32057267/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com