栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

springboot整合OSS实现文件简单上传

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

springboot整合OSS实现文件简单上传

springboot整合OSS实现文件简单上传

1、添加依赖

 		
            com.aliyun.oss
            aliyun-sdk-oss
            3.6.0
        

2、添加application配置。

aliyun:
  oss:
    url-scheme: http #(schema-如果用https的话,配置https)
    endpoint: oss-cn-beijing.aliyuncs.com # oss对外服务的访问域名
    accessKeyId: xxxxxxxxxxx # 访问身份验证中用到用户标识
    accessKeySecret: xxxxxxxxxxxxxx # 用户用于加密签名字符串和oss用来验证签名字符串的密钥
    bucketName: yeyoo # oss的存储空间
    policyExpire: 300 # 签名有效期(S)
    maxSize: 50 # 上传文件大小(M)
    dirPrefix: economic-brain/excel/ # 上传文件夹路径前缀 (需要按照自己项目配置)

3、创建OSS的相关java配置。

@Data
@Component
@ConfigurationProperties(
        prefix = "aliyun.oss"
)
public class OssProperties {
    public static final String OSS_PREFIX = "aliyun.oss";
    private String urlScheme;
    private String endpoint;
    private String accessKeyId;
    private String accessKeySecret;
    private String bucketName;
    private long policyExpire;
    @DataSizeUnit(DataUnit.MEGABYTES)
    private DataSize maxSize;
    private String dirPrefix;

    public OssProperties() {
    }

}

4、创建OssService。

public interface OssService {
   Map SimpleUpload(String filePath) throws Exception;
}

5、创建OssService实现类。

@Service
public class OssServiceImpl implements OssService {
    private static final Logger log = LoggerFactory.getLogger(OssServiceImpl.class);
    @Resource
    private OssProperties ossProperties;

    public OssServiceImpl() {
    }

    @Override
    public Map SimpleUpload(String filePath) {

        OSS ossClient = (new OSSClientBuilder()).build(this.ossProperties.getEndpoint(), this.ossProperties.getAccessKeyId(), this.ossProperties.getAccessKeySecret());
        File file = new File(filePath);
        String filename = file.getName();
        String key = this.excelDir()+"/"+filename;
        PutObjectRequest putObjectRequest = (PutObjectRequest)(new PutObjectRequest(this.ossProperties.getBucketName(), key, file));
        PutObjectResult result = ossClient.putObject(putObjectRequest);
        ossClient.shutdown();

        Map resultMap = new HashMap();
        resultMap.put("status",200);
        resultMap.put("fileName",filename);
        resultMap.put("filePath",key);

        return resultMap;

    }


    private String dir() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        return this.ossProperties.getDirPrefix() + sdf.format(new Date());
    }
    private String excelDir() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return this.ossProperties.getDirPrefix() + sdf.format(new Date());
    }
}

6、创建controller测试上传功能。

@RestController
@RequestMapping("/oss")
public class uploadOssController {

    @Resource
    private OssService ossService;

    @PostMapping({"/upload"})
    public Map upload() throws Exception {
        String filePath = "E:\down\downExcel\2021-08-09\企业基本信息(1625042741246).xlsx";
        Map result = this.ossService.SimpleUpload(filePath);
        return result;
    }

}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/318269.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号