tlias管理系统-配置文件-@ConfigurationProperties配置类对象(硬编码)功能实现
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package com.inmind.utils;
|
||||
|
||||
import com.aliyun.oss.ClientBuilderConfiguration;
|
||||
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.aliyun.oss.common.comm.SignVersion;
|
||||
import com.aliyun.oss.model.PutObjectRequest;
|
||||
import com.aliyun.oss.model.PutObjectResult;
|
||||
import com.aliyuncs.exceptions.ClientException;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.UUID;
|
||||
//备份
|
||||
@Component
|
||||
public class AliOSS1Utils {
|
||||
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
|
||||
/*private String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
|
||||
private String bucketName = "inmind-test1";
|
||||
private String region = "cn-hangzhou";*/
|
||||
|
||||
@Value("${aliyun.oss.endpoint}")
|
||||
private String endpoint;
|
||||
@Value("${aliyun.oss.bucketName}")
|
||||
private String bucketName;
|
||||
@Value("${aliyun.oss.region}")
|
||||
private String region ;
|
||||
|
||||
public String upload(MultipartFile file) throws IOException, ClientException {
|
||||
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
|
||||
//要使用uuid更换掉文件名,避免同名问题
|
||||
String filename = file.getOriginalFilename();//1.jpg 2.txt
|
||||
int index = filename.lastIndexOf(".");
|
||||
String extname = filename.substring(index);
|
||||
String objectName = UUID.randomUUID().toString()+extname;
|
||||
//地图片要修改为动态的文件上传的图片
|
||||
//获取上传文件的输入流
|
||||
InputStream inputStream = file.getInputStream();
|
||||
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
|
||||
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
|
||||
OSS ossClient = OSSClientBuilder.create()
|
||||
.endpoint(endpoint)
|
||||
.credentialsProvider(credentialsProvider)
|
||||
.clientConfiguration(clientBuilderConfiguration)
|
||||
.region(region)
|
||||
.build();
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, inputStream);
|
||||
PutObjectResult result = ossClient.putObject(putObjectRequest);
|
||||
ossClient.shutdown();
|
||||
//组装文件访问路径
|
||||
String url = endpoint.split("//")[0]+"//"+bucketName+"."+endpoint.split("//")[1]+"/"+objectName;//https://inmind-test1.oss-cn-hangzhou.aliyuncs.com/1.jpg
|
||||
return url;
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import com.aliyun.oss.common.comm.SignVersion;
|
||||
import com.aliyun.oss.model.PutObjectRequest;
|
||||
import com.aliyun.oss.model.PutObjectResult;
|
||||
import com.aliyuncs.exceptions.ClientException;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
@@ -19,19 +20,16 @@ import java.util.UUID;
|
||||
|
||||
@Component
|
||||
public class AliOSSUtils {
|
||||
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
|
||||
/*private String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
|
||||
private String bucketName = "inmind-test1";
|
||||
private String region = "cn-hangzhou";*/
|
||||
|
||||
@Value("${aliyun.oss.endpoint}")
|
||||
private String endpoint;
|
||||
@Value("${aliyun.oss.bucketName}")
|
||||
private String bucketName;
|
||||
@Value("${aliyun.oss.region}")
|
||||
private String region ;
|
||||
@Autowired
|
||||
private AliOssProperties aliOssProperties;
|
||||
|
||||
public String upload(MultipartFile file) throws IOException, ClientException {
|
||||
|
||||
String endpoint = aliOssProperties.getEndpoint();
|
||||
String bucketName = aliOssProperties.getBucketName();
|
||||
String region = aliOssProperties.getRegion();
|
||||
|
||||
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
|
||||
//要使用uuid更换掉文件名,避免同名问题
|
||||
String filename = file.getOriginalFilename();//1.jpg 2.txt
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.inmind.utils;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Data//条件二:提供get/set方法
|
||||
@Component//条件三:配置类对象交给IOC容器管理(spring容器)
|
||||
@ConfigurationProperties(prefix="aliyun.oss")//条件四:与配置文件中的前缀保持一致
|
||||
public class AliOssProperties {
|
||||
//条件一:属性于键值保持一致
|
||||
private String endpoint;
|
||||
private String bucketName;
|
||||
private String region;
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
# 应用服务 WEB 访问端口
|
||||
#server.port=8080
|
||||
#下面这些内容是为了让MyBatis映射
|
||||
#指定Mybatis的Mapper文件
|
||||
mybatis.mapper-locations=classpath:mappers/*xml
|
||||
#指定Mybatis的实体目录
|
||||
mybatis.type-aliases-package=com.inmind.mybatis.entity
|
||||
|
||||
#驱动类名称
|
||||
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
|
||||
#数据库连接的url
|
||||
spring.datasource.url=jdbc:mysql://localhost:3306/tlias1
|
||||
#连接数据库的用户名
|
||||
spring.datasource.username=root
|
||||
#连接数据库的密码
|
||||
spring.datasource.password=1234
|
||||
|
||||
#输出mybatis的日志
|
||||
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
|
||||
#开启mybatis的驼峰命名自动映射开关,将a_time ---->aTime
|
||||
mybatis.configuration.map-underscore-to-camel-case=true
|
||||
|
||||
#文件上传大小配置
|
||||
spring.servlet.multipart.max-file-size= 10MB
|
||||
spring.servlet.multipart.max-request-size=100MB
|
||||
|
||||
#阿里云OSS配置信息
|
||||
aliyun.oss.endpoint=https://oss-cn-hangzhou.aliyuncs.com
|
||||
aliyun.oss.bucketName=inmind-test1
|
||||
aliyun.oss.region=cn-hangzhou
|
||||
@@ -1,32 +1,29 @@
|
||||
server:
|
||||
port: 8080
|
||||
#mybatis连接四要素
|
||||
spring:
|
||||
datasource:
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
url: jdbc:mysql://localhost:3306/tlias1
|
||||
username: root
|
||||
password: 1234
|
||||
servlet:
|
||||
multipart:
|
||||
#单个文件上传大小限制10兆
|
||||
max-file-size: 10MB
|
||||
#一次请求多个文件上传大小限制100兆
|
||||
max-request-size: 100MB
|
||||
|
||||
#定义对象
|
||||
user:
|
||||
name: zhangsan
|
||||
age: 18
|
||||
password: 123456
|
||||
mybatis:
|
||||
configuration:
|
||||
#输出mybatis的日志
|
||||
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
|
||||
#驼峰自动映射
|
||||
map-underscore-to-camel-case: true
|
||||
|
||||
#定义map集合
|
||||
userMap:
|
||||
name: zhangsan
|
||||
age: 18
|
||||
password: 123456
|
||||
|
||||
#定义数组/list/set
|
||||
hobbys:
|
||||
- 吃饭
|
||||
- 睡觉
|
||||
- 玩手机
|
||||
|
||||
#定义对象数组 User[] 定义User对象的list
|
||||
users:
|
||||
- name: lisi
|
||||
age: 20
|
||||
gender: 1
|
||||
- name: wangwu
|
||||
age: 21
|
||||
gender: 2
|
||||
- name: zhaoliu
|
||||
age: 22
|
||||
gender: 1
|
||||
#阿里云OSS配置信息
|
||||
aliyun:
|
||||
oss:
|
||||
endpoint: https://oss-cn-hangzhou.aliyuncs.com
|
||||
bucketName: inmind-test1
|
||||
region: cn-hangzhou
|
||||
|
||||
Reference in New Issue
Block a user