gpt4 book ai didi

java - spring boot云存储文件上传

转载 作者:行者123 更新时间:2023-12-05 08:07:48 25 4
gpt4 key购买 nike

从 spring boot Controller 上传文件到谷歌云存储。请给我一个代码。

到目前为止我想出了这样的代码

@RequestMapping(value = "/uploadFile", method = RequestMethod.POST)
@SuppressWarnings("deprecation")
public String uploadFile( @RequestParam("files") Part filePart ) throws IOException {

private static Storage storage = null;

final String bucketName = "mcqimages";
DateTimeFormatter dtf = DateTimeFormat.forPattern("-YYYY-MM-dd-HHmmssSSS");
DateTime dt = DateTime.now(DateTimeZone.UTC);
String dtString = dt.toString(dtf);
String fileName = "C:\\Users\\sachinthah\\Downloads\\821092.png";
fileName = filePart.getSubmittedFileName() + dtString;

// the inputstream is closed by default, so we don't need to close it here
BlobInfo blobInfo =
storage.create(
BlobInfo
.newBuilder(bucketName, fileName)
// Modify access list to allow all users with link to read file
.setAcl(new ArrayList<>(Arrays.asList(Acl.of(User.ofAllUsers(), Role.READER))))
.build(),
filePart.getInputStream());

return blobInfo.getMediaLink();
}

最佳答案

有两个很好的来源,您可以从中获得有关将文件上传到谷歌云存储的指导,第一个是官方 GCP 指南使用 Java 云存储[1],第二个是谷歌云存储简介[2 ], 他们会一步步带你,并为你提供存档任务所需的代码。

[1] https://cloud.google.com/java/getting-started/using-cloud-storage[2] https://www.baeldung.com/java-google-cloud-storage

关于java - spring boot云存储文件上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53188177/

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