springboot中使用oss文件上传

  • 导入依赖
    <dependencies>
        <dependency>
            <groupId>com.aliyun.oss</groupId>
            <artifactId>aliyun-sdk-oss</artifactId>
        </dependency>
        <!-- 时间工具类 -->
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
        </dependency>
    </dependencies>
    
  • 编写配置
    server:
      port: 8002
    
    spring:
      application:
        name: service-oss
      profiles:
        active: dev
    
    aliyun:
      oss:
        file:
          endpoint: oss-cn-beijing.aliyuncs.com
          keyid: LTAI5tFVf5aGMAoSTiTABbuv
          keysecret: hqIWmRRSrZbMkuDrGJ0PbhTqHPwve1
          bucketname: guli-school-fanxing
    
  • 编写工具类用来读取配置文件的值
    @Data
    @Component
    @ConfigurationProperties(prefix = "aliyun.oss.file")
    public class OssConfigUtil {
    
        private String endpoint;
        private String keyid;
        private String keysecret;
        private String bucketname;
    
    }
    
  • service
    public interface FileService {
    
        public String uploadFileOss(MultipartFile file);
    }
    
  • service实现类
    @Service
    public class FileServiceImpl implements FileService{
    
        @Autowired
        private OssConfigUtil ossConfigUtil;
    
    
        @Override
        public String uploadFileOss(MultipartFile file) {
            OSS ossClient = null;
            try {
                String endpoint = ossConfigUtil.getEndpoint();
                String keyId = ossConfigUtil.getKeyid();
                String keySecret = ossConfigUtil.getKeysecret();
                String bucketName = ossConfigUtil.getBucketname();
    
                // 创建oss实例
                ossClient = new OSSClientBuilder().build(endpoint, keyId, keySecret);
    
                // 使用uuid防止同名文件覆盖,使用日期进行分类整理
                String originalFilename = file.getOriginalFilename();
                String fileName = UUID.randomUUID().toString().replace("-", "")+originalFilename;
                String filePath = new DateTime().toString("yyyy/MM/dd");
                String newFileName = filePath+"/"+fileName;
                // 文件上传
                ossClient.putObject(bucketName, newFileName, file.getInputStream());
                // 拼接url
                String url = "https://"+ bucketName +"."+ endpoint +"/"+ newFileName;
                return url;
            } catch (Exception e){
                e.printStackTrace();
                return null;
            } finally {
                // 关闭连接
                if (ossClient != null){
                    ossClient.shutdown();
                }
            }
        }
    }
    
  • controller
    @RestController
    @RequestMapping("/eduoss/file")
    @CrossOrigin
    public class FileController {
    
        @Autowired
        private FileService fileService;
    
        @PostMapping("upload")
        public R upload(MultipartFile file){
            String url = fileService.uploadFileOss(file);
            // R类是对返回结果的封装
            return R.ok().setData("url", url);
        }
    }
    
上一篇:516. 房屋染色 II


下一篇:Maven - 一篇带你理解 AnnotationProcessorPaths 使用