1.配置文件增加
aliyun:
oss:
endpoint: http://oss-cn-beijing.aliyuncs.com
accessKeyId : LTAI5tD2Znqu3RhnNMjiMVaB
accessKeySecret : Eje6S9L6Nz5KMRc2w0EFkBsqgvzHUA
2.创建工具类OssUtil
package com.peam.web.util;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyun.oss.model.OSSObject;
import com.peam.common.utils.uuid.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.Date;
@Component
public class OssUtil {
@Autowired
private OSSClient ossClient;
static String bucketName = "peam";
/**
* 上传文件到阿里云OSS
*
* @param file 要上传的文件
* @param folder 文件存储的目录
* @return 文件在OSS中的URL
*/
public String uploadFile(MultipartFile file, String folder) throws IOException {
String originalFilename = file.getOriginalFilename();
if (originalFilename == null || originalFilename.isEmpty()) {
// 文件名为空,处理异常情况,这里可以返回错误信息或抛出异常
throw new IllegalArgumentException("上传文件名为空");
}
// 获取文件后缀名
String fileExtension = "";
int dotIndex = originalFilename.lastIndexOf(".");
if (dotIndex > 0 && dotIndex < originalFilename.length() - 1) {
fileExtension = originalFilename.substring(dotIndex);
}
String simpleUUID = IdUtils.simpleUUID();
String fileName = simpleUUID + fileExtension; // 使用"ceshi"作为文件名称前缀,并加上后缀名
String key = folder + "/" + fileName;
ossClient.putObject(bucketName, key, file.getInputStream());
Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000); // 设置URL过期时间为1小时
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key);
request.setExpiration(expiration);
URL url = ossClient.generatePresignedUrl(request);
return url.toString();
}
/**
* 查看文件在OSS中的URL
*
* @return 文件在OSS中的URL
*/
public String getFileUrl(String url1) {
String key =url1;
Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000); // 设置URL过期时间为1小时
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key);
request.setExpiration(expiration);
URL url = ossClient.generatePresignedUrl(request);
return url.toString();
}
/**
* 查看文件在OSS中的URL
*
* @param folder 文件存储的目录
* @param fileName 文件名称
* @return 文件在OSS中的URL
*/
public String getFileUrl(String folder, String fileName) {
String key = folder + "/" + fileName;
Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000); // 设置URL过期时间为1小时
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, key);
request.setExpiration(expiration);
URL url = ossClient.generatePresignedUrl(request);
return url.toString();
}
/**
* 下载文件
*
* @param folder 文件存储的目录
* @param fileName 文件名称
* @return 文件流
*/
public InputStream downloadFile(String folder, String fileName) {
String key = folder + "/" + fileName;
OSSObject object = ossClient.getObject(bucketName, key);
return object.getObjectContent();
}
}
3.开始使用
后端接口示例
//上传文件
@PostMapping("/saveFile")
public R<String> saveFile(MultipartFile file) throws IOException {
String s = ossUtil.uploadFile(file, "ceshi");
return R.ok(s);
}
//根据路径查看
@GetMapping ("/getFileUrl")
public R<String> saveFile(String url) throws IOException {
String s = ossUtil.getFileUrl(url);
return R.ok(s);
}