javaEEday10-tlias管理系统-配置文件-@ConfigurationProperties
This commit is contained in:
@@ -7,6 +7,7 @@ import com.aliyun.oss.common.comm.SignVersion;
|
|||||||
import com.aliyun.oss.model.PutObjectRequest;
|
import com.aliyun.oss.model.PutObjectRequest;
|
||||||
import com.aliyun.oss.model.PutObjectResult;
|
import com.aliyun.oss.model.PutObjectResult;
|
||||||
import com.aliyuncs.exceptions.ClientException;
|
import com.aliyuncs.exceptions.ClientException;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.web.multipart.MultipartFile;
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
@@ -20,14 +21,21 @@ import java.util.UUID;
|
|||||||
@Component
|
@Component
|
||||||
public class AliOSSUtils {
|
public class AliOSSUtils {
|
||||||
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
|
// Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
|
||||||
@Value("${aliyun.oss.endpoint}")
|
/* @Value("${aliyun.oss.endpoint}")
|
||||||
private String endpoint;
|
private String endpoint;
|
||||||
@Value("${aliyun.oss.bucketName}")
|
@Value("${aliyun.oss.bucketName}")
|
||||||
private String bucketName;
|
private String bucketName;
|
||||||
@Value("${aliyun.oss.region}")
|
@Value("${aliyun.oss.region}")
|
||||||
private String region;
|
private String region;*/
|
||||||
|
@Autowired
|
||||||
|
private AliOssProperties aliOssProperties;
|
||||||
|
|
||||||
public String upload(MultipartFile file) throws IOException, ClientException {
|
public String upload(MultipartFile file) throws IOException, ClientException {
|
||||||
|
//偷懒操作
|
||||||
|
String endpoint = aliOssProperties.getEndpoint();
|
||||||
|
String region = aliOssProperties.getRegion();
|
||||||
|
String bucketName = aliOssProperties.getBucketName();
|
||||||
|
|
||||||
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
|
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
|
||||||
//要使用uuid更换掉文件名,避免同名问题
|
//要使用uuid更换掉文件名,避免同名问题
|
||||||
String filename = file.getOriginalFilename();//1.jpg 2.txt
|
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
|
||||||
|
@Component //将当前类的对象交给IOC--spring容器来管理
|
||||||
|
@ConfigurationProperties(prefix = "aliyun.oss")
|
||||||
|
public class AliOssProperties {
|
||||||
|
private String endpoint;
|
||||||
|
private String bucketName;
|
||||||
|
private String region;
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user