当前位置:   article > 正文

阿里云图片文件上传

阿里云图片文件上传

一,官网地址

https://help.aliyun.com/document_detail/84781.html

一切依据于官网

二,导入依赖

  1. <dependencies>
  2. <!-- 阿里云oss依赖 -->
  3. <dependency>
  4. <groupId>com.aliyun.oss</groupId>
  5. <artifactId>aliyun-sdk-oss</artifactId>
  6. </dependency>
  7. <!-- 日期工具栏依赖 -->
  8. <dependency>
  9. <groupId>joda-time</groupId>
  10. <artifactId>joda-time</artifactId>
  11. </dependency>
  12. </dependencies>

三,创建配置文件

  1. #服务端口
  2. server.port=8205
  3. #服务名
  4. spring.application.name=service-oss
  5. #环境设置:dev、test、prod
  6. spring.profiles.active=dev
  7. #阿里云 OSS
  8. #不同的服务器,地址不同
  9. aliyun.oss.file.endpoint=oss-cn-beijing.aliyuncs.com
  10. aliyun.oss.file.keyid=your accessKeyId
  11. aliyun.oss.file.keysecret=your accessKeySecret
  12. #bucket可以在控制台创建,也可以使用java代码创建
  13. aliyun.oss.file.bucketname=guli-file

四创建启动类

  1. import org.springframework.boot.SpringApplication;
  2. import org.springframework.boot.autoconfigure.SpringBootApplication;
  3. import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
  4. import org.springframework.context.annotation.ComponentScan;
  5. @SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
  6. @ComponentScan("com.atguigu")
  7. public class OssApplication {
  8. public static void main(String[] args) {
  9. SpringApplication.run(OssApplication.class,args);
  10. }
  11. }

五,创建常量类

由于环境的不同,配置需要变化,因此需要将配置文件变为软编码而不是写在硬编码里面,耦合性较高

  1. import org.springframework.beans.factory.InitializingBean;
  2. import org.springframework.beans.factory.annotation.Value;
  3. import org.springframework.stereotype.Component;
  4. @Component
  5. public class ConstantPropertiesUtil implements InitializingBean {
  6. @Value("${aliyun.oss.file.endpoint}")
  7. private String endpoint;
  8. @Value("${aliyun.oss.file.keyid}")
  9. private String keyId;
  10. @Value("${aliyun.oss.file.keysecret}")
  11. private String keySecret;
  12. @Value("${aliyun.oss.file.bucketname}")
  13. private String bucketName;
  14. public static String END_POINT;
  15. public static String ACCESS_KEY_ID;
  16. public static String ACCESS_KEY_SECRET;
  17. public static String BUCKET_NAME;
  18. @Override
  19. public void afterPropertiesSet() throws Exception {
  20. END_POINT = endpoint;
  21. ACCESS_KEY_ID = keyId;
  22. ACCESS_KEY_SECRET = keySecret;
  23. BUCKET_NAME = bucketName;
  24. }
  25. }

六,创建controller→service

  1. import com.atguigu.yygh.common.R;
  2. import com.atguigu.yygh.oss.service.OssService;
  3. import io.swagger.annotations.Api;
  4. import io.swagger.annotations.ApiOperation;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.web.bind.annotation.PostMapping;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RestController;
  9. import org.springframework.web.multipart.MultipartFile;
  10. @Api(tags = "文件上传接口")
  11. @RestController
  12. @RequestMapping("/admin/oss/file")
  13. public class OssController {
  14. @Autowired
  15. private OssService ossService;
  16. /**
  17. * @Description: 上传文件
  18. * @return: 文件的url
  19. * @param:文件流
  20. * @author: Mr zhan
  21. */
  22. @ApiOperation("上传图片到阿里云")
  23. @PostMapping("upload")
  24. public R upload(MultipartFile file) {
  25. //传递到service
  26. String fileUrl=ossService.upload(file);
  27. return R.ok().data("file",fileUrl);
  28. }
  29. }
  1. import com.aliyun.oss.OSS;
  2. import com.aliyun.oss.OSSClientBuilder;
  3. import com.atguigu.yygh.oss.service.OssService;
  4. import com.atguigu.yygh.oss.util.ConstantPropertiesUtil;
  5. import org.joda.time.DateTime;
  6. import org.springframework.stereotype.Service;
  7. import org.springframework.web.multipart.MultipartFile;
  8. import java.io.IOException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.Date;
  11. import java.util.UUID;
  12. @Service
  13. public class OssServiceImpl implements OssService {
  14. /**
  15. * @Description: 上传文件
  16. * @return: 文件的url
  17. * @param:文件流
  18. * @author: Mr zhan
  19. */
  20. @Override
  21. public String upload(MultipartFile file) {
  22. //1.获取文件的名称
  23. String filename = file.getOriginalFilename();
  24. //2.准备参数
  25. //地域结点
  26. String endpoint=ConstantPropertiesUtil.END_POINT;
  27. //唯一id
  28. String accessKeyId=ConstantPropertiesUtil.ACCESS_KEY_ID;
  29. //id的对应的唯一secret
  30. String accessKeySecret=ConstantPropertiesUtil.ACCESS_KEY_SECRET;
  31. //存储桶
  32. String bucketName=ConstantPropertiesUtil.BUCKET_NAME;
  33. //文件存储的路径
  34. // String objectName="用下面的filename代替";
  35. //3.创建客户端
  36. OSS ossClient= new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
  37. String uuid = UUID.randomUUID().toString().replaceAll("-","");
  38. filename=uuid+filename;
  39. //4.由于真实的开发需要,方便运营,因此需要分类格式如下: 2022/03/05/图片名称
  40. SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
  41. String month = format.format(new Date());
  42. filename=month+"/"+filename;
  43. //上传
  44. try {
  45. ossClient.putObject(bucketName,filename,file.getInputStream());
  46. } catch (IOException e) {
  47. e.printStackTrace();
  48. }finally {
  49. //关闭资源
  50. ossClient.shutdown();
  51. }
  52. return "https://"+bucketName+"."+endpoint+"/"+filename;
  53. }
  54. }
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/873625
推荐阅读
相关标签
  

闽ICP备14008679号