当前位置:网站首页>OSS简单上传图片
OSS简单上传图片
2022-07-30 19:50:00 【ldj2020】
package com.atguigu.oss.service.impl;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.atguigu.oss.service.OssService;
import com.atguigu.oss.utils.ConstantPropertiesUtils;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
@Service
public class OssServiceImpl implements OssService {
//上传头像到oss
@Override
public String uploadFileAvatar(MultipartFile file) {
// 工具类获取常量值也可以配置到配置文件@Value(${xxx})获取
String endpoint = ConstantPropertiesUtils.END_POIND;
String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
String bucketName = ConstantPropertiesUtils.BUCKET_NAME;
try {
// 创建OSS实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
//获取上传文件输入流
InputStream inputStream = file.getInputStream();
//获取文件名称
String fileName = file.getOriginalFilename();
String uuid = UUID.randomUUID().toString().replaceAll("-", "");
fileName = uuid + fileName;
String datePath = new DateTime().toString("yyyy/MM/dd");
fileName = datePath + "/" + fileName;
/**
* 调用oss方法实现上传
* 第一个参数 Bucket名称
* 第二个参数 上传到oss文件路径和文件名称
* 第三个参数 上传文件输入流
*/
ossClient.putObject(bucketName, fileName, inputStream);
// 关闭OSSClient。
ossClient.shutdown();
//需要把上传到阿里云oss路径手动拼接出来
return "https://" + bucketName + "." + endpoint + "/" + fileName;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
边栏推荐
- .eslintrc.js for musicApp
- SQLyog注释 添加 撤销 快捷键
- 【Node实现数据加密】
- VBA runtime error '-2147217900 (80040e14): Automation error
- 从离线到实时对客,湖仓一体释放全量数据价值
- MindSpore:【语音识别】DFCNN网络训练loss不收敛
- Entering the applet for the first time
- [flink] Error finishing Could not instantiate the executor. Make sure a planner module is on the classpath
- Linux下安装MySQL教程
- After MySQL grouping, take the largest piece of data [optimal solution]
猜你喜欢

DCM 中间件家族迎来新成员

推荐系统:冷启动问题【用户冷启动、物品冷启动、系统冷启动】

Different lower_case_table_names settings for server (‘1‘) and data dictionary (‘0‘) 解决方案

These services can't ali interview?Then don't go to, the basic notification, etc

SimpleOSS third-party library libcurl and engine libcurl error solution

MySql密码

MySQL数据库 ---MySQL表的增删改查(进阶)
Database Tuning - Database Tuning

MindSpore:【JupyterLab】按照新手教程训练时报错

Database indexes: indexes are not a panacea
随机推荐
MySQL mass production of data
ELK log analysis system
[Node implements data encryption]
移动web开发01
MindSpore:【MindSpore1.1】Mindspore安装后验证出现cudaSetDevice failed错误
牛客网——华为题库(100~108)
Talking about Contrastive Learning (Contrastive Learning) the first bullet
PHP低代码开发平台 V5.0.7新版发布
MindSpore:【模型训练】【mindinsight】timeline的时间和实际用时相差很远
使用MULTISET来比较数据集的实例介绍
“数字化重构系统,搞定 CEO 是第一步”
MySQL database - DQL data query language
el-input can only input integers (including positive numbers, negative numbers, 0) or only integers (including positive numbers, negative numbers, 0) and decimals
MySQL kills 10 questions, how many questions can you stick to?
LeetCode 0952. Calculate Maximum Component Size by Common Factor: Mapping / Union Search
How to build FTP server under win2003
Range.CopyFromRecordset method (Excel)
VBA runtime error '-2147217900 (80040e14): Automation error
Day31 LeetCode
HCIP --- 企业网的三层架构