阿里云OSS图片上传详解
【代码】阿里云OSS图片上传详解。
·
目录
3.申请成功之后可以获取到相对应的code码,然后把以下配置粘贴到配置文件
1.进入以下链接进行申请
2.导入依赖
<!--OSS图片上传-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.16.3</version>
</dependency>
3.申请成功之后可以获取到相对应的code码,然后把以下配置粘贴到配置文件
oss:
endPoint: 你自己的
accessKeyId: 你自己的
accessKeySecret: 你自己的
accessPre: 你自己的
bucketName: 你自己的
4.上传图片工具类(已经封装好直接使用即可)
package com.shopgoods.coupon.util;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.PutObjectRequest;
import lombok.Data;
import lombok.extern.log4j.Log4j2;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.time.LocalDateTime;
import java.util.UUID;
/**
* Oss服务调用
*/
@Log4j2
@Configuration
@ConfigurationProperties(prefix = "oss")
@Data
public class OssUtil {
/**
* Endpoint 存储对象概述 阿里云主账号AccessKey,accessKeySecret拥有所有API的访问权限 访问路径前缀 存储对象概述
*/
private String endPoint;
private String accessKeyId;
private String accessKeySecret;
private String accessPre;
/**
* bucket名称
*
* @return
*/
private String bucketName;
/**
* 构建 OSS 对象
*
* @return
*/
private OSS initOssClient() {
return new OSSClientBuilder().build(
endPoint,
accessKeyId,
accessKeySecret);
}
/**
* 默认路径上传本地文件
*
* @param filePath
*/
public String uploadFile(String filePath) {
return uploadFileForBucket(bucketName, getOssFilePath(filePath), filePath);
}
/**
* 默认路径上传multipartFile文件
*
* @param multipartFile
*/
public String uploadMultipartFile(MultipartFile multipartFile) {
return uploadMultipartFile(bucketName, getOssFilePath(multipartFile.getOriginalFilename()), multipartFile);
}
/**
* 上传 multipartFile 类型文件
*
* @param bucketName
* @param ossPath
* @param multipartFile
*/
public String uploadMultipartFile(String bucketName, String ossPath, MultipartFile multipartFile) {
InputStream inputStream = null;
try {
inputStream = multipartFile.getInputStream();
} catch (IOException e) {
e.printStackTrace();
}
uploadFileInputStreamForBucket(bucketName, ossPath, inputStream);
return accessPre + ossPath;
}
/**
* 使用File上传PutObject上传文件 ** 程序默认使用次方法上传
*
* @param bucketName 实例名称
* @param ossPath oss存储路径
* @param filePath 本地文件路径
*/
public String uploadFileForBucket(String bucketName, String ossPath, String filePath) {
// 创建PutObjectRequest对象。
PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, ossPath, new File(filePath));
// 上传
initOssClient().putObject(putObjectRequest);
return accessPre + ossPath;
}
/**
* 使用文件流上传到指定的bucket实例
*
* @param bucketName 实例名称
* @param ossPath oss存储路径
* @param filePath 本地文件路径
*/
public String uploadFileInputStreamForBucket(String bucketName, String ossPath, String filePath) {
// 填写本地文件的完整路径。如果未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件流。
InputStream inputStream = null;
try {
inputStream = new FileInputStream(filePath);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
// 填写Bucket名称和Object完整路径。Object完整路径中不能包含Bucket名称。
uploadFileInputStreamForBucket(bucketName, ossPath, inputStream);
return accessPre + ossPath;
}
public void uploadFileInputStreamForBucket(String bucketName, String ossPath, InputStream inputStream) {
initOssClient().putObject(bucketName, ossPath, inputStream);
}
/**
* 下载
*
* @param ossFilePath
* @param filePath
*/
public void downloadFile(String ossFilePath, String filePath) {
downloadFileForBucket(bucketName, ossFilePath, filePath);
}
/**
* 下载
*
* @param bucketName 实例名称
* @param ossFilePath oss存储路径
* @param filePath 本地文件路径
*/
public void downloadFileForBucket(String bucketName, String ossFilePath, String filePath) {
initOssClient().getObject(new GetObjectRequest(bucketName, ossFilePath), new File(filePath));
}
/**
* @return
*/
public String getOssDefaultPath() {
LocalDateTime now = LocalDateTime.now();
String url =
now.getYear() + "/" +
now.getMonth() + "/" +
now.getDayOfMonth() + "/" +
now.getHour() + "/" +
now.getMinute() + "/";
return url;
}
/**
* 对文件进行重新命名
*
* @param filePath
* @return
*/
public String getOssFilePath(String filePath) {
String fileSuf = filePath.substring(filePath.lastIndexOf(".") + 1);
return getOssDefaultPath() + UUID.randomUUID().toString() + "." + fileSuf;
}
}
5.控制层注入工具类,调用方法
@Autowired
private OssUtil ossUtil;
/**
* 图片上传
* @param file
* @return
*/
@PostMapping("/upload")
public String upload(MultipartFile file){
String filePath = ossUtil.uploadMultipartFile(file);
return filePath;
}
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)