阿里云图片文件上传

ops/2024/9/24 5:35:46/

一,官网地址

https://help.aliyun.com/document_detail/84781.html

一切依据于官网

二,导入依赖

<dependencies><!-- 阿里云oss依赖 --><dependency><groupId>com.aliyun.oss</groupId><artifactId>aliyun-sdk-oss</artifactId></dependency><!-- 日期工具栏依赖 --><dependency><groupId>joda-time</groupId><artifactId>joda-time</artifactId></dependency>
</dependencies>

三,创建配置文件

#服务端口
server.port=8205
#服务名
spring.application.name=service-oss#环境设置:dev、test、prod
spring.profiles.active=dev#阿里云 OSS
#不同的服务器,地址不同
aliyun.oss.file.endpoint=oss-cn-beijing.aliyuncs.com
aliyun.oss.file.keyid=your accessKeyId
aliyun.oss.file.keysecret=your accessKeySecret
#bucket可以在控制台创建,也可以使用java代码创建
aliyun.oss.file.bucketname=guli-file

四创建启动类

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
@ComponentScan("com.atguigu")
public class OssApplication {public static void main(String[] args) {SpringApplication.run(OssApplication.class,args);}
}

五,创建常量类

由于环境的不同,配置需要变化,因此需要将配置文件变为软编码而不是写在硬编码里面,耦合性较高

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;@Component
public class ConstantPropertiesUtil implements InitializingBean {@Value("${aliyun.oss.file.endpoint}")private String endpoint;@Value("${aliyun.oss.file.keyid}")private String keyId;@Value("${aliyun.oss.file.keysecret}")private String keySecret;@Value("${aliyun.oss.file.bucketname}")private String bucketName;public static String END_POINT;public static String ACCESS_KEY_ID;public static String ACCESS_KEY_SECRET;public static String BUCKET_NAME;@Overridepublic void afterPropertiesSet() throws Exception {END_POINT = endpoint;ACCESS_KEY_ID = keyId;ACCESS_KEY_SECRET = keySecret;BUCKET_NAME = bucketName;}
}

六,创建controller→service


import com.atguigu.yygh.common.R;
import com.atguigu.yygh.oss.service.OssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;@Api(tags = "文件上传接口")
@RestController
@RequestMapping("/admin/oss/file")
public class OssController {@Autowiredprivate OssService ossService;/*** @Description: 上传文件* @return:  文件的url* @param:文件流* @author: Mr zhan*/@ApiOperation("上传图片到阿里云")@PostMapping("upload")public R upload(MultipartFile file) {//传递到serviceString fileUrl=ossService.upload(file);return R.ok().data("file",fileUrl);}
}
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.atguigu.yygh.oss.service.OssService;
import com.atguigu.yygh.oss.util.ConstantPropertiesUtil;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;@Service
public class OssServiceImpl implements OssService {/*** @Description: 上传文件* @return:  文件的url* @param:文件流* @author: Mr zhan*/@Overridepublic String upload(MultipartFile file) {//1.获取文件的名称String filename = file.getOriginalFilename();//2.准备参数//地域结点String endpoint=ConstantPropertiesUtil.END_POINT;//唯一idString accessKeyId=ConstantPropertiesUtil.ACCESS_KEY_ID;//id的对应的唯一secretString accessKeySecret=ConstantPropertiesUtil.ACCESS_KEY_SECRET;//存储桶String bucketName=ConstantPropertiesUtil.BUCKET_NAME;//文件存储的路径//   String objectName="用下面的filename代替";//3.创建客户端OSS ossClient= new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);String uuid = UUID.randomUUID().toString().replaceAll("-","");filename=uuid+filename;//4.由于真实的开发需要,方便运营,因此需要分类格式如下:  2022/03/05/图片名称SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");String month = format.format(new Date());filename=month+"/"+filename;//上传try {ossClient.putObject(bucketName,filename,file.getInputStream());} catch (IOException e) {e.printStackTrace();}finally {//关闭资源ossClient.shutdown();}return "https://"+bucketName+"."+endpoint+"/"+filename;}
}

http://www.ppmy.cn/ops/85912.html

相关文章

Live800:跨渠道客户服务:无缝衔接,随时随地满足客户需求

在当今这个数字化快速发展的时代&#xff0c;客户对于服务的需求已经不再局限于单一渠道。他们希望通过多种渠道&#xff0c;如电话、电子邮件、社交媒体、在线聊天等&#xff0c;随时随地获得所需的服务。因此&#xff0c;跨渠道客户服务成为了企业提升客户满意度和忠诚度的关…

labview四字节转浮点数

1.labview四字节转浮点数 2.Labview怎么把串口接收到的数据转换成浮点数&#xff1f; Labview怎么把串口接收到的数据转换成浮点数&#xff1f;

2024 微信小程序 学习笔记 第二天

1. WXML 模板语法 数据绑定 事件绑定 条件渲染 列表渲染 2. WXSS 模板样式 rpx 样式导入 全局和局部样式 3. 全局配置 window tabBar 配置tabBar案例 4. 网络数据请求 Get请求 Post 请求 加载时请求 5. 案例 -本地生活&#xff08;首页&#xff09; 导航栏 轮播图 九宫格效果…

披荆斩棘:Python开发者在市场低迷期快速找到工作的策略

披荆斩棘&#xff1a;Python开发者在市场低迷期快速找到工作的策略 在瞬息万变的科技领域&#xff0c;市场低迷期对各个领域的专业人士来说都充满了挑战。Python开发者以其灵活性和专业性著称&#xff0c;但也无法完全避免经济波动的影响。然而&#xff0c;通过采取正确的策略…

如何使用 SQLite ?

SQLite 是一个轻量级、嵌入式的关系型数据库管理系统&#xff08;RDBMS&#xff09;。它是一种 C 库&#xff0c;实现了自给自足、无服务器、零配置、事务性 SQL 数据库引擎。SQLite 的源代码是开放的&#xff0c;完全在公共领域。它被广泛用于各种应用程序&#xff0c;包括浏览…

面完英伟达算法岗,心态崩了。。。

最近这一两周看到不少互联网公司都已经开始秋招提前批了。不同以往的是&#xff0c;当前职场环境已不再是那个双向奔赴时代了。求职者在变多&#xff0c;HC 在变少&#xff0c;岗位要求还更高了。 最近&#xff0c;我们又陆续整理了很多大厂的面试题&#xff0c;帮助一些球友解…

【Linux】-----工具篇(yum介绍)

目录 Ⅰ、是什么&#xff1f; Ⅱ、Linux下安装软件的三种方式 ①源代码安装 ②rpm包安装 ③yum安装 Ⅲ、yum相关操作 1.查看软件包 2.安装软件 3.卸载软件 Ⅳ、yum本地配置 Ⅰ、是什么&#xff1f; yum是包管理器&#xff0c;也就像一个软件下载安装管理的客户端&…

软件测试---网络基础、HTTP

一、网络基础 &#xff08;1&#xff09;Web和网络知识 网络基础TCP/IP 使用HTTP协议访问Web WWW万维网的诞生 WWW万维网的构成 &#xff08;2&#xff09;IP协议 &#xff08;3&#xff09;可靠传输的TCP和三次握手策略 &#xff08;4&#xff09;域名解析服务DNS &#xff0…