当前位置:   article > 正文

Spring Boot 笔记 019 创建接口_文件上传

Spring Boot 笔记 019 创建接口_文件上传

1.1 创建阿里OSS bucket

OSS Java SDK 兼容性和示例代码_对象存储(OSS)-阿里云帮助中心 (aliyun.com)

1.2 编写工具类

  1. package com.geji.utils;
  2. import com.aliyun.oss.ClientException;
  3. import com.aliyun.oss.OSS;
  4. import com.aliyun.oss.OSSClientBuilder;
  5. import com.aliyun.oss.OSSException;
  6. import com.aliyun.oss.model.PutObjectRequest;
  7. import com.aliyun.oss.model.PutObjectResult;
  8. import java.io.FileInputStream;
  9. import java.io.InputStream;
  10. public class AliOssUtil {
  11. // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
  12. private static final String ENDPOINT = "XXXXXXXXXXXXXXXXXXXXXX";
  13. // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
  14. //EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
  15. private static final String ACCESS_KEY_ID="XXXXXXXXXXXXXXXXXXXXXXX";
  16. private static final String ACCESS_KEY_SECRET="XXXXXXXXXXXXXXXXXXXXXXXXXXXX";
  17. // 填写Bucket名称,例如examplebucket。
  18. private static final String BUCKET_NAME = "XXXXXXXXXXXXXXXXXXXXXXXXXXXX";
  19. public static String uploadFile(String objectName,InputStream in) throws Exception {
  20. // 创建OSSClient实例。
  21. //OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
  22. OSS ossClient = new OSSClientBuilder().build(ENDPOINT, ACCESS_KEY_ID,ACCESS_KEY_SECRET);
  23. String url="";
  24. try {
  25. // 填写字符串。
  26. String content = "Hello OSS,你好世界";
  27. // 创建PutObjectRequest对象。
  28. PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, objectName, in);
  29. // 如果需要上传时设置存储类型和访问权限,请参考以下示例代码。
  30. // ObjectMetadata metadata = new ObjectMetadata();
  31. // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
  32. // metadata.setObjectAcl(CannedAccessControlList.Private);
  33. // putObjectRequest.setMetadata(metadata);
  34. // 上传字符串。
  35. PutObjectResult result = ossClient.putObject(putObjectRequest);
  36. url = "https://"+BUCKET_NAME+"."+ENDPOINT.substring(ENDPOINT.lastIndexOf("/")+1)+"/"+objectName;
  37. } catch (OSSException oe) {
  38. System.out.println("Caught an OSSException, which means your request made it to OSS, "
  39. + "but was rejected with an error response for some reason.");
  40. System.out.println("Error Message:" + oe.getErrorMessage());
  41. System.out.println("Error Code:" + oe.getErrorCode());
  42. System.out.println("Request ID:" + oe.getRequestId());
  43. System.out.println("Host ID:" + oe.getHostId());
  44. } catch (ClientException ce) {
  45. System.out.println("Caught an ClientException, which means the client encountered "
  46. + "a serious internal problem while trying to communicate with OSS, "
  47. + "such as not being able to access the network.");
  48. System.out.println("Error Message:" + ce.getMessage());
  49. } finally {
  50. if (ossClient != null) {
  51. ossClient.shutdown();
  52. }
  53. }
  54. return url;
  55. }
  56. }

1.3 创建文件上传controller

  1. package com.geji.controller;
  2. import com.geji.pojo.Result;
  3. import com.geji.utils.AliOssUtil;
  4. import org.springframework.web.bind.annotation.PostMapping;
  5. import org.springframework.web.bind.annotation.RestController;
  6. import org.springframework.web.multipart.MultipartFile;
  7. import java.io.File;
  8. import java.io.IOException;
  9. import java.util.UUID;
  10. @RestController
  11. public class FileUploadController {
  12. @PostMapping("/upload")
  13. public Result<String> upload(MultipartFile file) throws Exception {
  14. String originalFilename=file.getOriginalFilename();
  15. String filename= UUID.randomUUID().toString()+originalFilename.substring(originalFilename.lastIndexOf("."));
  16. String url= AliOssUtil.uploadFile(filename,file.getInputStream());
  17. return Result.success(url);
  18. }
  19. }

1.4 postman测试

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/86602?site
推荐阅读
相关标签
  

闽ICP备14008679号