1、文件上传模块整合阿里云oss服务文件上传
This commit is contained in:
parent
0158b62f88
commit
2c6d0fe295
1
pom.xml
1
pom.xml
|
|
@ -47,6 +47,7 @@
|
|||
<hutool.version>5.7.17</hutool.version>
|
||||
<mybatisplus.version>3.4.3.4</mybatisplus.version>
|
||||
<commonsNet.version>3.6</commonsNet.version>
|
||||
<aliyunoss.version>3.14.0</aliyunoss.version>
|
||||
|
||||
</properties>
|
||||
|
||||
|
|
|
|||
|
|
@ -61,6 +61,13 @@
|
|||
<version>${commonsNet.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!--阿里云oss-->
|
||||
<dependency>
|
||||
<groupId>com.aliyun.oss</groupId>
|
||||
<artifactId>aliyun-sdk-oss</artifactId>
|
||||
<version>${aliyunoss.version}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- RuoYi Api System -->
|
||||
<dependency>
|
||||
<groupId>com.ruoyi</groupId>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,38 @@
|
|||
package com.ruoyi.file.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 阿里云oss配置属性
|
||||
*
|
||||
* @author xiejs
|
||||
* @since 2022-01-25
|
||||
*/
|
||||
@Data
|
||||
@Component
|
||||
@ConfigurationProperties(prefix = "aliyun.oss.file")
|
||||
public class AliyunOssProperties {
|
||||
|
||||
/**
|
||||
* 文件地址
|
||||
*/
|
||||
private String endpoint;
|
||||
|
||||
/**
|
||||
*keyId
|
||||
*/
|
||||
private String keyId;
|
||||
|
||||
/**
|
||||
* 密钥
|
||||
*/
|
||||
private String keySecret;
|
||||
|
||||
/**
|
||||
* 文件分类
|
||||
*/
|
||||
private String bucketName;
|
||||
|
||||
}
|
||||
|
|
@ -0,0 +1,69 @@
|
|||
package com.ruoyi.file.service;
|
||||
|
||||
import cn.hutool.core.date.DateUtil;
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.aliyun.oss.OSS;
|
||||
import com.aliyun.oss.OSSClientBuilder;
|
||||
import com.ruoyi.file.config.AliyunOssProperties;
|
||||
import com.ruoyi.file.utils.FileUploadUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 阿里云oss文件上传实现
|
||||
* @author xiejs
|
||||
* @since 2022-01-25
|
||||
*/
|
||||
@Service
|
||||
@Primary
|
||||
public class AliyunOssFileServiceImpl implements ISysFileService{
|
||||
|
||||
|
||||
@Autowired
|
||||
private AliyunOssProperties aliyunOssProperties;
|
||||
|
||||
@Override
|
||||
public String uploadFile(MultipartFile file) throws Exception {
|
||||
try {
|
||||
String endpoint = aliyunOssProperties.getEndpoint();
|
||||
String keyId = aliyunOssProperties.getKeyId();
|
||||
String keySecret = aliyunOssProperties.getKeySecret();
|
||||
String bucketName = aliyunOssProperties.getBucketName();
|
||||
OSS ossClient = new OSSClientBuilder().build(endpoint,
|
||||
keyId, keySecret);
|
||||
//获取流
|
||||
InputStream is = file.getInputStream();
|
||||
//获取文件后缀
|
||||
String extension = FileUploadUtils.getExtension(file);
|
||||
//获取文件名称
|
||||
String fileName = getDataTime()+"."+extension;
|
||||
//执行文件上传 bucket名称 文件名称 文件流
|
||||
ossClient.putObject(bucketName,fileName,is);
|
||||
//关闭ossClient
|
||||
ossClient.shutdown();
|
||||
//拼接文件地址
|
||||
return "https://"+bucketName+"."+endpoint+"/"+fileName;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成一个当前日期文件名
|
||||
* @return
|
||||
*/
|
||||
private String getDataTime(){
|
||||
String today = DateUtil.format(new Date(), "yyyy-MM");
|
||||
String time = DateUtil.formatDateTime(new Date());
|
||||
int random = RandomUtil.randomInt(1000, 10000);
|
||||
//防止同一时间生成文件名重复
|
||||
return today+"/"+time+"-"+random;
|
||||
}
|
||||
}
|
||||
|
|
@ -5,7 +5,6 @@ import com.ruoyi.file.config.FtpProperties;
|
|||
import com.ruoyi.file.utils.FileUploadUtils;
|
||||
import com.ruoyi.file.utils.FtpUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
|
@ -18,7 +17,6 @@ import java.util.UUID;
|
|||
* @since 2022-01-24
|
||||
*/
|
||||
@Service
|
||||
@Primary
|
||||
public class FtpSysFileServiceImpl implements ISysFileService{
|
||||
|
||||
@Autowired
|
||||
|
|
|
|||
Loading…
Reference in New Issue