springBoot接入阿里云oss的实现步骤
作者:保持上靖心 时间:2023-05-28 23:09:04
maven导入依赖
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.15</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- 阿里云OSS -->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
</dependencies>
定义阿里云上传结果实体
package com.example.demo.entity;
import lombok.Data;
/**
* 阿里云上传结果集
*
* @author wushuai
* @create 2021-01-25
*/
@Data
public class AliyunOssResult {
/**
* code:200成功
* code: 400失败
*/
private int code;
/**
* 上传成功的返回url
*/
private String url;
/**
* 提示信息
*/
private String msg;
}
yml设置阿里云oss参数
aliyunOss:
endpoint: "http://oss-cn-shanghai.aliyuncs.com"
accessKeyId: "xxxxxxx"
accessKeySecret: "xxxxxxx"
bucketName: "xxxxxx"
urlPrefix: "http://bucketName.oss-cn-shanghai.aliyuncs.com/"
yml设置上传文件大小限制
spring:
servlet:
multipart:
max-file-size: 20MB
max-request-size: 20MB
工具类封装
package com.example.demo.util;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.DeleteObjectsRequest;
import com.aliyun.oss.model.DeleteObjectsResult;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.example.demo.entity.AliyunOssResult;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
import java.util.List;
@Component
public class AliyunOSSUtil {
@Value("${aliyunOss.endpoint}")
private String endpoint;
@Value("${aliyunOss.accessKeyId}")
private String accessKeyId;
@Value("${aliyunOss.accessKeySecret}")
private String accessKeySecret;
@Value("${aliyunOss.bucketName}")
private String bucketName;
@Value("${aliyunOss.urlPrefix}")
private String urlPrefix;
/**
* 上传文件,以IO流方式
*
* @param inputStream 输入流
* @param objectName 唯一objectName(在oss中的文件名字)
*/
public AliyunOssResult upload(InputStream inputStream, String objectName) {
AliyunOssResult aliyunOssResult = new AliyunOssResult();
try {
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 上传内容到指定的存储空间(bucketName)并保存为指定的文件名称(objectName)。
ossClient.putObject(bucketName, objectName, inputStream);
// 关闭OSSClient。
ossClient.shutdown();
aliyunOssResult.setCode(200);
aliyunOssResult.setUrl(urlPrefix+objectName);
aliyunOssResult.setMsg("上传成功");
} catch (Exception e) {
e.printStackTrace();
aliyunOssResult.setCode(400);
aliyunOssResult.setMsg("上传失败");
}
return aliyunOssResult;
}
/**
* 删除OSS中的单个文件
*
* @param objectName 唯一objectName(在oss中的文件名字)
*/
public void delete(String objectName) {
try {
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 删除文件。
ossClient.deleteObject(bucketName, objectName);
// 关闭OSSClient。
ossClient.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 批量删除OSS中的文件
*
* @param objectNames oss中文件名list
*/
public void delete(List<String> objectNames) {
try {
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 批量删除文件。
DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(objectNames));
List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();
// 关闭OSSClient。
ossClient.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取文件临时url
*
* @param objectName oss中的文件名
* @param effectiveTime 有效时间(ms)
*/
public String getUrl(String objectName,long effectiveTime){
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 设置URL过期时间
Date expiration = new Date(new Date().getTime() + effectiveTime);
GeneratePresignedUrlRequest generatePresignedUrlRequest ;
generatePresignedUrlRequest =new GeneratePresignedUrlRequest(bucketName, objectName);
generatePresignedUrlRequest.setExpiration(expiration);
URL url = ossClient.generatePresignedUrl(generatePresignedUrlRequest);
return url.toString();
}
}
controller接收调用
package com.example.demo.controller;
import com.example.demo.util.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.util.UUID;
@RestController
@RequestMapping("/file")
public class FileController {
@Autowired
private AliyunOSSUtil aliyunOSSUtil;
@RequestMapping(value = "/uploadFile")
public @ResponseBody
Object uploadFile(@RequestParam(value = "file", required = false) MultipartFile file,
String strPath) throws IOException {
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".") + 1);
String objectName = strPath+"/"+ UUID.randomUUID().toString()+"."+suffix;
return aliyunOSSUtil.upload(file.getInputStream(),objectName);
}
}
postman测试
来源:https://blog.csdn.net/qq_41541368/article/details/113114086
标签:springBoot,阿里云oss
0
投稿
猜你喜欢
Java实现抠图片文字或签名的完整代码
2023-04-18 00:04:44
Java操作Excel的示例详解
2021-07-08 00:56:56
c++野指针的原理以及避免方法
2023-10-07 09:18:53
磁盘配额的wmi版本(C#)
2022-07-31 17:55:17
Mybatis如何实现@Select等注解动态组合SQL语句
2022-04-13 16:42:05
java安全编码指南之:表达式规则说明
2021-06-01 17:48:44
推荐两款java开发实用工具 hutool 和 lombok
2022-04-06 10:20:14
java后台接受到图片后保存方法
2023-06-03 09:23:04
在Java的Hibernate框架中使用SQL语句的简单介绍
2022-06-13 15:54:15
IDEA修改idea64.exe.vmoptions文件以及解决coding卡顿问题
2022-11-02 00:36:04
获取JsonObject某一未知key的值操作
2023-02-10 15:09:45
c# 钩子学习笔记
2023-03-31 20:54:22
通过Java实现设置Word文档页边距的方法详解
2022-04-07 14:00:31
实战SpringBoot集成JWT实现token验证
2022-10-07 15:57:49
浅谈图片上传利用request.getInputStream()获取文件流时遇到的问题
2023-10-18 10:36:43
springboot连接sqllite遇到的坑及解决
2023-04-27 04:37:38
Android系统view与SurfaceView的基本使用及区别分析
2023-04-08 00:33:26
深入理解Android Bitmap
2023-07-29 08:52:14
详解java CountDownLatch和CyclicBarrier在内部实现和场景上的区别
2022-03-22 13:03:02
spring boot 全局异常处理方法汇总
2021-07-06 22:44:04