阿里云OSS-上传
程序员文章站
2022-07-14 21:48:52
...
pom
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.5.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
yml配置
aliyun:
oss:
access-key-id: LTAI4GKkr***qioSkJmo
access-key-secret: Y8oWPnT***lXlrrlyH4lAq5X
bucket-domain: http://***.oss-cn-beijing.aliyuncs.com
bucket-name: ***
end-point: http://oss-cn-beijing.aliyuncs.com
工具类
package com.qyc.oss;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.common.comm.ResponseMessage;
import com.aliyun.oss.model.PutObjectResult;
import java.beans.SimpleBeanInfo;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
/**
* @author qyc
* @time 2020/6/24 - 16:41
*/
public class OSSUtil {
public static boolean uploadFileToOss(
String endpoint,String accessKeyId,String accessKeySecret,
InputStream inputStream,String bucketName,String bucketDomain,String originalName){
OSS ossclent = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
String folderName = new SimpleDateFormat("yyyMMdd").format(new Date());
String fileMainName = UUID.randomUUID().toString().replace("-","");
String extensionName = originalName.substring(originalName.lastIndexOf("."));
String objectName = folderName+"/"+fileMainName+extensionName;
try{
PutObjectResult putObjectResult = ossclent.putObject(bucketName,objectName,inputStream);
ResponseMessage responseMessage = putObjectResult.getResponse();
if(responseMessage == null){
//资源路径
// https://qyc-oostest.oss-cn-beijing.aliyuncs.com/20200624/2b9f74b42a6c4f7f87d846fa4a3cdfad.png
String ossFileAccessPath = bucketDomain+"/"+objectName;
return true;
}else {
int statusCode = responseMessage.getStatusCode();
String errorMessage = responseMessage.getErrorResponseAsString();
return false;
}
}catch (Exception e){
return false;
}finally {
if(ossclent!=null){
ossclent.shutdown();
}
}
}
}
controller
@Controller
public class OssController {
@Autowired
private OSSProperties ossProperties;
@RequestMapping("/oss/update")
public String oss(MultipartFile file) throws IOException {
// https://qyc-oostest.oss-cn-beijing.aliyuncs.com/20200624/2b9f74b42a6c4f7f87d846fa4a3cdfad.png
boolean stat = OSSUtil.uploadFileToOss(
ossProperties.getEndPoint(),
ossProperties.getAccessKeyId(),
ossProperties.getAccessKeySecret(),
file.getInputStream(),
ossProperties.getBucketName(),
ossProperties.getBucketDomain(),
file.getOriginalFilename());
return "success";
}
}
page
<form action="/oss/update" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="上传">
</form>
<img src="https://qyc-oostest.oss-cn-beijing.aliyuncs.com/20200624/2b9f74b42a6c4f7f87d846fa4a3cdfad.png" sizes="50">
</body>
</html>
上一篇: 最近公司培训的算法
下一篇: 简单动态代理实现AOP AOPJDK