工业自动化网
发布采购单
官方微信 精彩呈现

SpringBoot完美配置阿里云的文件上传

   日期:2020-07-26     浏览:53    
核心提示:新建一个config类AliyunOSS.java@Configuration @Data public class AliyunOSS { private OSSClient ossClient; @Value(${images
新建一个config类
AliyunOSS.java
@Configuration
@Data
public class AliyunOSS {
private OSSClient ossClient;


@Value("${images-roland.file.endpoint}")
private String endpoint;

@Value("${images-roland.file.keyid}")
private String accessKeyId;

@Value("${images-roland.file.keysecret}")
private String secretAccessKey;

@Value("${images-roland.file.filehost}")
private String file_filehost;

@Value("${images-roland.file.bucketname1}")
private String bucketname1;

@Bean("ossClients")
public OSSClient ossClient(){
return new OSSClient(endpoint,accessKeyId,secretAccessKey);
}
}

然后在yml中配置你的阿里云信息
images-roland:
file:
endpoint: oss-cn-shenzhen.aliyuncs.com
keyid: ********
keysecret: ********
bucketname: roland
filehost: images/
show_image_host: https:/
@Slf4j
@Component
public class AliyunOSSUtil {


private static OSSClient ossClients;

@Autowired
public AliyunOSSUtil(OSSClient ossClients) {
AliyunOSSUtil.ossClients = ossClients;
}

public static String upload(File file){
log.info("=========>OSS文件上传开始:"+file.getName());
// System.out.println(ossClients);

String bucketName=ConstantProperties.BUCKET_NAME1;
String fileHost=ConstantProperties.FILE_HOST;

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = format.format(new Date());

if(null == file){
return "";
}

OSSClient ossClient = ossClients;
System.out.println(ossClient);
try {
//容器不存在,就创建
if(! ossClient.doesBucketExist(bucketName)){
ossClient.createBucket(bucketName);
CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
ossClient.createBucket(createBucketRequest);
}
//创建文件路径
String fileUrl = fileHost+"/"+(dateStr + "/" + UUID.randomUUID().toString().replace("-","")+"-"+file.getName());
//上传文件
PutObjectResult result = ossClient.putObject(new PutObjectRequest(bucketName, fileUrl, file));
//设置权限 这里是公开读
ossClient.setBucketAcl(bucketName,CannedAccessControlList.PublicRead);
if(null != result){
log.info("==========>OSS文件上传成功,OSS地址:"+fileUrl);
return fileUrl;
}
}catch (OSSException oe){
log.error(oe.getMessage());

}catch (ClientException ce){
log.error(ce.getMessage());

}finally {
//关闭
// ossClient.shutdown();
}
return null;
}
public static String uploads(File file){
String bucketName=ConstantProperties.BUCKET_NAME1;
String fileHost=ConstantProperties.FILE_HOST;

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = format.format(new Date());

if(null == file){
return null;
}

// OSSClient ossClient = ossClients;

// 创建OSSClient实例。
OSSClient ossClient = ossClients;
String fileUrl = fileHost+"/"+(dateStr + "/" + UUID.randomUUID().toString().replace("-","")+"-"+file.getName());
try {
// 带进度条的上传。
ossClient.putObject(new PutObjectRequest(bucketName, fileUrl, new FileInputStream(file)).
<PutObjectRequest>withProgressListener(new PutObjectProgressListener()));

} catch (Exception e) {
e.printStackTrace();
}
// 关闭OSSClient。
// ossClient.shutdown();
return null;
}
}
 
打赏
 
更多>同类技术

推荐图文
推荐技术
点击排行
工业自动化网
产品检索: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Processed in 1,597,263,532.030 second(s), 15 queries, Memory 0.32 M