当前位置:网站首页>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;
}
}
}
边栏推荐
- Interviewer Ali: Describe to me the phenomenon of cache breakdown, and talk about your solution?
- [Private Series] All kinds of strange problems encountered in daily PHP
- Database Tuning - Database Tuning
- 推荐系统-模型:FNN模型(FM+MLP=FNN)
- The 17th "Revitalization Cup" National Youth Vocational Skills Competition - Computer Programmers (Cloud Computing Platform and Operation and Maintenance) Participation Review and Summary
- Talking about Contrastive Learning (Contrastive Learning) the first bullet
- Start foreground Activity
- MySQL performance optimization (hardware, system configuration, table structure, SQL statements)
- Linux下最新版MySQL 8.0的下载与安装(详细步骤)
- Another company interview
猜你喜欢
MindSpore:【JupyterLab】查看数据时报错
如何优化OpenSumi终端性能?
Snowflake vs. Redshift的2022战报:两个数据平台谁更适合你?
Download Win11 how to change the default path?Download Win11 change the default path method
MySQl数据库————DQL数据查询语言
来了!东方甄选为龙江农产品直播带货
阿里面试官:给我描述一下缓存击穿的现象,并说说你的解决思路?
MySQL database - views and indexes
[PyTorchVideo Tutorial 01] Quickly implement video action recognition
MindSpore:ImageFolderDataset数据读取问题
随机推荐
The JDBC programming of the MySQL database
mysql慢查询优化
Entering the applet for the first time
MySQL database master-slave configuration
Day31 LeetCode
M3SDA:用于多源域自适应的矩匹配
MySQL数据库之JDBC编程
刷题记录----字符串
win2003下FTP服务器如何搭建
Difference between Object and Map
历史上的今天:Win10 七周年;微软和雅虎的搜索协议;微软发行 NT 4.0
Install Mysql5.7 under Linux, super detailed and complete tutorial, and cloud mysql connection
湖仓一体电商项目(四):项目数据种类与采集
After MySQL grouping, take the largest piece of data [optimal solution]
从离线到实时对客,湖仓一体释放全量数据价值
MySQL mass production of data
The advanced version of the cattle brushing series (search for rotating sorted arrays, inversion of the specified range in the linked list)
MySQL分组后取最大一条数据【最优解】
iPhone真是十三香?两代产品完全对比,或许上一代更值得买
Zabbix 5.0 监控教程(一)