- #阿里云oss配置
- aliyun.oss.endpoint=https://oss-cn-beijing.aliyuncs.com
- aliyun.oss.bucketName=hzleadnews-01
- @Value ( "${aliyun.oss.endpoint}" )
- private String endpoint ;
- @Value ( "${aliyun.oss.bucketName}" )
- private String bucketName ;
- #数据库连接信息
- spring:
- datasource:
- driver-class-name: com.mysql.cj.jdbc.Driver
- url: jdbc:mysql://localhost:3306/tlias
- username: lyx
- password: 123456
- #配置文件上传
- servlet:
- multipart:
- max-file-size: 10MB
- max-request-size: 100MB
- #Mybatis配置
- mybatis:
- configuration:
- log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
- map-underscore-to-camel-case: true
- #阿里云OSS配置
- aliyun:
- oss:
- endpoint: https://oss-cn-beijing.aliyuncs.com
- bucketName: hzleadnews-01
自动注入属性值
@ConfigurationProperties 自动注入配置文件中的前缀
- import lombok.Data;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
-
- @Data
- @Component
- @ConfigurationProperties(prefix ="aliyun.oss")
- public class AliOSSPropertis {
- private String endpoint ;
- private String bucketName ;
- }
- import com.aliyun.oss.OSS;
- import com.aliyun.oss.OSSClientBuilder;
- import com.aliyun.oss.common.auth.CredentialsProviderFactory;
- import com.aliyun.oss.common.auth.EnvironmentVariableCredentialsProvider;
- import com.aliyuncs.exceptions.ClientException;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Value;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.stereotype.Component;
- import org.springframework.web.multipart.MultipartFile;
- import java.io.*;
- import java.util.UUID;
-
- /**
- * 阿里云 OSS 工具类
- */
-
-
- public class AliOSSUtils {
- // @Value ( "${aliyun.oss.endpoint}" )
- // private String endpoint ;
- // @Value ( "${aliyun.oss.bucketName}" )
- // private String bucketName ;
- @Autowired
- private AliOSSPropertis aliOSSPropertis;
-
-
-
-
-
-
-
- /**
- * 实现上传图片到OSS
- */
- public String upload(MultipartFile file) throws Exception {
- //获取OSS参数
- String endpoint = aliOSSPropertis.getEndpoint ();
- String bucketName = aliOSSPropertis.getBucketName ();
- EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
- // 获取上传的文件的输入流
- InputStream inputStream = file.getInputStream();
-
- // 避免文件覆盖
- String originalFilename = file.getOriginalFilename();
- String fileName = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
-
- //上传文件到 OSS
- OSS ossClient = new OSSClientBuilder().build(endpoint, credentialsProvider);
- ossClient.putObject(bucketName, fileName, inputStream);
-
- //文件访问路径
- String url = endpoint.split("//")[0] + "//" + bucketName + "." + endpoint.split("//")[1] + "/" + fileName;
- // 关闭ossClient
- ossClient.shutdown();
- return url;// 把上传到oss的路径返回
- }
-
- }
- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-configuration-processorartifactId>
- dependency>
2.引入依赖后配置yml文件时会有提示