阿里云官方文档:https://help.aliyun.com/document_detail/32008.html?spm=a2c4g.11186623.6.915.3140503fKPJLqD
一定要学会看文档!!!
1 需求
首先登录阿里云官网,找到对象存储oss 创建Bucket
点击之后 其他都是默认 只需要把读写权限 改为 公共读即可 点击确定就创建完成了
2 编写
然后下面来编写代码 首先创建一个springboot 应用程序 在 pom.xml 文件中导入以下依赖
<!-- aliyun-oos -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
<!-- 时间工具 -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.9</version>
</dependency>
在application.yml 中配置
spring:
profiles:
active: dev
application:
name: oss-file
aliyun:
oss:
endpoint: oss-cn-shenzhen.aliyuncs.com
keyId: AccessKey中的keyid
keySecret: AccessKey的keySecret
bucketName: aly-file-210824
keyId 和 keySecret 在AccessKey 管理里面 去创建一个即可
编写一个配置类
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* Created with IntelliJ IDEA.
*
* @Project: aliyun
* @Package: com.znsd.aliyun.alyunossfile.uilt
* @Author: zhangrongjie
* @Date: 2021/8/24 22:51
* @Description:
*/
@Component //加入spring ioc 容器
@Data //get set
@ConfigurationProperties(prefix = "aliyun.oss")
public class OssProperties {
private String endpoint; //外网访问 地域节点
private String keyId;
private String keySecret;
private String bucketName; //kuctet 列表名称
}
编写service 接口
import java.io.InputStream;
/**
* Created with IntelliJ IDEA.
*
* @Project: aliyun
* @Package: com.znsd.aliyun.alyunossfile.service
* @Author: zhangrongjie
* @Date: 2021/8/24 22:55
* @Description:
*/
public interface FileService {
/**
* 描述
* @param inputStream 文件流
* @param module bucket列表中文件夹名称
* @param oFilename 上传的文件名称
* @return java.lang.String
* @author zhangrongjie
* @date 2021/8/24
* @version 1.0
* @Description //TODO
*/
String upload(InputStream inputStream,String module,String oFilename);
}
service实现
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.znsd.aliyun.alyunossfile.service.FileService;
import com.znsd.aliyun.alyunossfile.uilt.OssProperties;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.InputStream;
import java.util.UUID;
/**
* Created with IntelliJ IDEA.
*
* @Project: aliyun
* @Package: com.znsd.aliyun.alyunossfile.service.impl
* @Author: zhangrongjie
* @Date: 2021/8/24 22:57
* @Description:
*/
@Service
public class FileServiceImpl implements FileService {
@Autowired
private OssProperties ossProperties; //配置类
@Override
public String upload(InputStream inputStream, String module, String oFilename) {
String keyId = ossProperties.getKeyId();
String keySecret = ossProperties.getKeySecret();
String endpoint = ossProperties.getEndpoint();
String bucketName = ossProperties.getBucketName();
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, keyId, keySecret);
ObjectMetadata meta = new ObjectMetadata();
meta.setContentType(getContentType(oFilename)); //设置http头
//文件路径
String folder = new DateTime().toString("yyyy/MM/dd");//日期目录
String extentionName = oFilename.substring(oFilename.lastIndexOf(".")); //拓展名称
String fileName = UUID.randomUUID().toString();
String key = module + "/" + folder + "/" + fileName + extentionName;
// 依次填写Bucket名称(例如examplebucket)和Object完整路径(例如exampledir/exampleobject.txt)。Object完整路径中不能包含Bucket名称。
ossClient.putObject(bucketName, key, inputStream, meta);
// 关闭OSSClient。
ossClient.shutdown();
//返回url地址
return "https://"+bucketName+"."+endpoint+"/"+key;
}
// 根据上传的文件后缀返回对应请求头
public static String getContentType(String FilenameExtension) {
if (FilenameExtension.equalsIgnoreCase(".bmp")) {
return "image/bmp";
}
if (FilenameExtension.equalsIgnoreCase(".gif")) {
return "image/gif";
}
if (FilenameExtension.equalsIgnoreCase(".jpeg") ||
FilenameExtension.equalsIgnoreCase(".jpg") ||
FilenameExtension.equalsIgnoreCase(".png")) {
return "image/jpg";
}
if (FilenameExtension.equalsIgnoreCase(".html")) {
return "text/html";
}
if (FilenameExtension.equalsIgnoreCase(".txt")) {
return "text/plain";
}
if (FilenameExtension.equalsIgnoreCase(".vsd")) {
return "application/vnd.visio";
}
if (FilenameExtension.equalsIgnoreCase(".pptx") ||
FilenameExtension.equalsIgnoreCase(".ppt")) {
return "application/vnd.ms-powerpoint";
}
if (FilenameExtension.equalsIgnoreCase(".docx") ||
FilenameExtension.equalsIgnoreCase(".doc")) {
return "application/msword";
}
if (FilenameExtension.equalsIgnoreCase(".xml")) {
return "text/xml";
}
return "image/jpg";
}
}
最后编写controller 运行测试
@RestController
public class FileController {
@Autowired
private FileService fileService;
@PostMapping("upload")
public String upload(@RequestParam("file")MultipartFile file,@RequestParam("module")String module) throws IOException {
InputStream inputStream = file.getInputStream(); //文件流
String originalFilename = file.getOriginalFilename(); //文件名称
String url = fileService.upload(inputStream,module,originalFilename);
return url;
}
}
3 测试
运行之后选择文件上传 返回一个url 就上传完成了
访问这个url 就可以预览 刚刚上传的图片了
最后我们可以看到我们阿里云文件管理里面就上传进去了这个文件