first commit
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
package com.starry.common.utils.file;
|
||||
|
||||
|
||||
import com.qcloud.cos.COSClient;
|
||||
import com.qcloud.cos.model.PutObjectRequest;
|
||||
import com.qcloud.cos.model.UploadResult;
|
||||
import com.qcloud.cos.transfer.TransferManager;
|
||||
import com.qcloud.cos.transfer.Upload;
|
||||
import com.starry.common.config.CosConfig;
|
||||
import com.starry.common.utils.SpringUtils;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.ArrayBlockingQueue;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @author admin
|
||||
* 腾讯云COS文件上传工具类
|
||||
* @since 2022/10/31
|
||||
*/
|
||||
@Slf4j
|
||||
public class CosClientUtils {
|
||||
|
||||
/**
|
||||
* 获取配置信息
|
||||
*/
|
||||
private static final CosConfig cosConfig = SpringUtils.getBean(CosConfig.class);
|
||||
|
||||
public static String upload(MultipartFile file, String dir) throws Exception {
|
||||
String originalFilename = file.getOriginalFilename();
|
||||
// 文件名
|
||||
String name = FilenameUtils.getBaseName(originalFilename) + "_" + System.currentTimeMillis() + originalFilename.substring(originalFilename.lastIndexOf("."));
|
||||
// 目录
|
||||
String folderName = cosConfig.getFolderPrefix() + "/" + dir + "/";
|
||||
String key = folderName + name;
|
||||
File localFile = null;
|
||||
try {
|
||||
localFile = transferToFile(file);
|
||||
String filePath = uploadFileToCos(localFile, key);
|
||||
log.info("upload COS successful: {}", filePath);
|
||||
return filePath;
|
||||
} catch (Exception e) {
|
||||
throw new Exception("文件上传失败");
|
||||
} finally {
|
||||
localFile.delete();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 用缓冲区来创建临时文件
|
||||
* 使用 MultipartFile.transferTo()
|
||||
*
|
||||
* @param multipartFile
|
||||
* @return
|
||||
*/
|
||||
private static File transferToFile(MultipartFile multipartFile) throws IOException {
|
||||
String originalFilename = multipartFile.getOriginalFilename();
|
||||
String prefix = originalFilename.split("\\.")[0];
|
||||
String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
|
||||
File file = File.createTempFile(prefix, suffix);
|
||||
multipartFile.transferTo(file);
|
||||
return file;
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传文件到COS
|
||||
*
|
||||
* @param localFile
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
private static String uploadFileToCos(File localFile, String key) throws InterruptedException {
|
||||
PutObjectRequest putObjectRequest = new PutObjectRequest(cosConfig.getBucketName(), key, localFile);
|
||||
// 获取连接
|
||||
COSClient cosClient = cosConfig.getCosClient();
|
||||
// 创建线程池
|
||||
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(8, 16,
|
||||
4, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10), new ThreadPoolExecutor.AbortPolicy());
|
||||
// 传入一个threadPool, 若不传入线程池, 默认TransferManager中会生成一个单线程的线程池
|
||||
TransferManager transferManager = new TransferManager(cosClient, threadPool);
|
||||
// 返回一个异步结果Upload, 可同步的调用waitForUploadResult等待upload结束, 成功返回UploadResult, 失败抛出异常
|
||||
Upload upload = transferManager.upload(putObjectRequest);
|
||||
UploadResult uploadResult = upload.waitForUploadResult();
|
||||
transferManager.shutdownNow();
|
||||
cosClient.shutdown();
|
||||
String filePath = cosConfig.getBaseUrl() + uploadResult.getKey();
|
||||
return filePath;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.starry.common.utils.file;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* @author admin
|
||||
* 文件上传工具类
|
||||
* @since 2022/10/28
|
||||
*/
|
||||
public class FileUploadUtils {
|
||||
|
||||
/**
|
||||
* 默认大小 50M
|
||||
*/
|
||||
public static final long DEFAULT_MAX_SIZE = 50 * 1024 * 1024;
|
||||
|
||||
/**
|
||||
* 默认的文件名最大长度 100
|
||||
*/
|
||||
public static final int DEFAULT_FILE_NAME_LENGTH = 100;
|
||||
|
||||
public static String upload(String baseDir, MultipartFile file, String[] allowedExtension) {
|
||||
int fileNameLength = Objects.requireNonNull(file.getOriginalFilename()).length();
|
||||
if (fileNameLength > FileUploadUtils.DEFAULT_FILE_NAME_LENGTH) {
|
||||
throw new RuntimeException("文件名超过默认的最大长度");
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user