|
|
@@ -7,7 +7,6 @@ import com.sckw.core.utils.IdWorker;
|
|
|
import com.sckw.core.utils.PasswordUtils;
|
|
|
import com.sckw.core.utils.StringUtils;
|
|
|
import com.sckw.excel.common.NumberConstant;
|
|
|
-import com.sckw.excel.common.StringConstant;
|
|
|
import com.sckw.file.common.enums.AliyunOssFileTypeEnum;
|
|
|
import com.sckw.file.common.enums.FileEnum;
|
|
|
import jakarta.annotation.PostConstruct;
|
|
|
@@ -92,6 +91,7 @@ public class FileUtils {
|
|
|
|
|
|
/**
|
|
|
* 获取指定文件或文件夹的后缀名
|
|
|
+ *
|
|
|
* @param fileName 文件名称
|
|
|
* @return
|
|
|
*/
|
|
|
@@ -109,10 +109,10 @@ public class FileUtils {
|
|
|
*/
|
|
|
public static Map<String, String> uploadFileByInfo(MultipartFile file, FileEnum fileEnum) {
|
|
|
Map<String, String> infoMap = new HashMap<>();
|
|
|
+ //创建OSSClient实例
|
|
|
+ defalutOSS();
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
try {
|
|
|
- //创建OSSClient实例
|
|
|
- defalutOSS();
|
|
|
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
//容器不存在,就创建
|
|
|
if (!ossClient.doesBucketExist(bucketName)) {
|
|
|
ossClient.createBucket(bucketName);
|
|
|
@@ -134,7 +134,7 @@ public class FileUtils {
|
|
|
//2021/02/02/01.jpg
|
|
|
String timeUrl = new DateTime().toString("yyyyMMdd");
|
|
|
fileName = timeUrl + "/" + fileName;
|
|
|
- String filePath = BASE_DIR + fileName+fileSuffix;
|
|
|
+ String filePath = BASE_DIR + fileName + fileSuffix;
|
|
|
//调用方法实现上传
|
|
|
ossClient.putObject(bucketName, filePath, inputStream);
|
|
|
//上传后的文件地址
|
|
|
@@ -144,14 +144,19 @@ public class FileUtils {
|
|
|
//上传之后文件路径
|
|
|
//https://yygh-atguigu.oss-cn-beijing.aliyuncs.com/01.jpg
|
|
|
// String url = "https://" + bucketName + "." + endpoint + "/" + filePath;
|
|
|
- String url = StringConstant.HTTPS_STRING+StringConstant.COLON+StringConstant.LEFT_SEPARATORS + bucketName + StringConstant.POINT + endpoint + StringConstant.LEFT_SEPARATOR+ filePath;
|
|
|
+ //绝对路径 "https://kaiwu-saas.oss-cn-chengdu.aliyuncs.com/+ filePath;"
|
|
|
+// String url = StringConstant.HTTPS_STRING+StringConstant.COLON+StringConstant.LEFT_SEPARATORS + bucketName + StringConstant.POINT + endpoint + StringConstant.LEFT_SEPARATOR+ filePath;
|
|
|
+ //相对路径
|
|
|
+ String url = filePath;
|
|
|
//返回 上传文件地址
|
|
|
infoMap.put("filePath", url);
|
|
|
} catch (IOException e) {
|
|
|
infoMap.put("filePath", null);
|
|
|
+ //关闭OSSClient。
|
|
|
+ ossClient.shutdown();
|
|
|
e.printStackTrace();
|
|
|
log.error("oss-upload-file-error:{}", e.getMessage(), e);
|
|
|
- throw new RuntimeException("oss-upload-file-error:"+e.getMessage());
|
|
|
+ throw new RuntimeException("oss-upload-file-error:" + e.getMessage());
|
|
|
}
|
|
|
return infoMap;
|
|
|
}
|
|
|
@@ -166,6 +171,9 @@ public class FileUtils {
|
|
|
* @return
|
|
|
*/
|
|
|
public static String uploadFile(MultipartFile file, FileEnum fileEnum) {
|
|
|
+ // 私有云要关闭CNAME
|
|
|
+ ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
|
|
|
+ conf.setSupportCname(false);
|
|
|
try {
|
|
|
//创建OSSClient实例
|
|
|
defalutOSS();
|
|
|
@@ -190,8 +198,11 @@ public class FileUtils {
|
|
|
String timeUrl = new DateTime().toString("yyyyMMdd");
|
|
|
fileName = timeUrl + "/" + fileName;
|
|
|
String filePath = BASE_DIR + fileName;
|
|
|
+ /**设置上传内容类型*/
|
|
|
+ ObjectMetadata objectMeta = new ObjectMetadata();
|
|
|
+ objectMeta.setContentType(FileUtils.getContentTypeByOSS(file.getOriginalFilename()));
|
|
|
//调用方法实现上传
|
|
|
- ossClient.putObject(bucketName, filePath, inputStream);
|
|
|
+ ossClient.putObject(bucketName, filePath, inputStream,objectMeta);
|
|
|
//上传后的文件地址
|
|
|
// String url1 = getUrl(ossClient, bucketName, filePath);
|
|
|
// System.out.println(url1);
|
|
|
@@ -361,19 +372,19 @@ public class FileUtils {
|
|
|
*
|
|
|
* @param response response
|
|
|
* @param fileName 文件名字,带后缀,例子:postman.txt
|
|
|
- * 文件全路径 https://kaiwu-saas.oss-cn-chengdu.aliyuncs.com/kll/uploads/20230605/146325493677821952598454132.txt
|
|
|
+ * 文件全路径 kll/uploads/20230605/146325493677821952598454132.txt 去除“https://kaiwu-saas.oss-cn-chengdu.aliyuncs.com/”
|
|
|
*/
|
|
|
public static void downloadByFileName(HttpServletResponse response, String fileName) {
|
|
|
defalutOSS();
|
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
OSSObject ossObject = ossClient.getObject(bucketName, fileName);
|
|
|
boolean exist = ossClient.doesObjectExist(bucketName, fileName);
|
|
|
- if (!exist){
|
|
|
+ if (!exist) {
|
|
|
throw new RuntimeException("下载文件不存在!");
|
|
|
}
|
|
|
String contentType = ossObject.getObjectMetadata().getContentType();
|
|
|
String contentType1 = getContentType(fileName);
|
|
|
- System.out.println("contentType:" + contentType);
|
|
|
+// System.out.println("contentType:" + contentType);
|
|
|
// System.out.println("contentType1:" + contentType1);
|
|
|
//设置响应内容类型,当设置了ContentType为“image/jpg”时,浏览器可以直接显示图片;
|
|
|
response.setContentType(contentType);
|
|
|
@@ -575,7 +586,7 @@ public class FileUtils {
|
|
|
*/
|
|
|
public static String getUrl(OSS ossClient, String bucketName, String key) {
|
|
|
//设置URl过期时间为99年:3600L*1000*24*365*99
|
|
|
- Date expiration = new Date( System.currentTimeMillis()+ 3600L * 1000 * 24 * 365 * 99);
|
|
|
+ Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 99);
|
|
|
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, key);
|
|
|
generatePresignedUrlRequest.setExpiration(expiration);
|
|
|
URL url = ossClient.generatePresignedUrl(generatePresignedUrlRequest);
|
|
|
@@ -605,7 +616,7 @@ public class FileUtils {
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent()));
|
|
|
while (true) {
|
|
|
String line = reader.readLine();
|
|
|
- if (line == null){
|
|
|
+ if (line == null) {
|
|
|
break;
|
|
|
}
|
|
|
|
|
|
@@ -671,6 +682,7 @@ public class FileUtils {
|
|
|
|
|
|
/**
|
|
|
* 通过oss的完整key下载
|
|
|
+ *
|
|
|
* @param response response
|
|
|
* @param url 全路径的url地址 kll/uploads/20230621/07ccbec381a011d121a215719199ac49.html
|
|
|
*/
|
|
|
@@ -679,13 +691,13 @@ public class FileUtils {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * @Title:downloadToFile
|
|
|
- * @Description: 下载文件到本地
|
|
|
* @param @param ossFilePath oss上的文件路径(全路径)
|
|
|
* @param @param newFilePath 本地文件路径 (全路径)
|
|
|
* @return void 返回类型
|
|
|
+ * @Title:downloadToFile
|
|
|
+ * @Description: 下载文件到本地
|
|
|
*/
|
|
|
- public static void downloadToFile(String endPoint, String accessKeyId, String accessKeySecret, String bucketName, String ossFilePath, String newFilePath){
|
|
|
+ public static void downloadToFile(String endPoint, String accessKeyId, String accessKeySecret, String bucketName, String ossFilePath, String newFilePath) {
|
|
|
//实例化OSSClient对象
|
|
|
OSSClient ossClient = (OSSClient) new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
|
|
|
try {
|
|
|
@@ -859,6 +871,7 @@ public class FileUtils {
|
|
|
|
|
|
/**
|
|
|
* 删除文件
|
|
|
+ *
|
|
|
* @param url 示例:'download/file.xsl' oss服务器文件路径以及文件名
|
|
|
*/
|
|
|
public static void remove(String url) {
|
|
|
@@ -885,17 +898,18 @@ public class FileUtils {
|
|
|
|
|
|
/**
|
|
|
* 获取OSS连接
|
|
|
+ *
|
|
|
* @return
|
|
|
*/
|
|
|
private static void getOSSClient() {
|
|
|
- if (ossclient == null){
|
|
|
+ if (ossclient == null) {
|
|
|
ossclient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 根据key删除OSS服务器上的文件
|
|
|
- *
|
|
|
+ * <p>
|
|
|
* param ossClient oss连接
|
|
|
* param bucketName 存储空间
|
|
|
* param folder 模拟文件夹名 如"qj_nanjing/"
|
|
|
@@ -956,7 +970,7 @@ public class FileUtils {
|
|
|
* 下载文件,此处主要是给Controller层提供一个OSS的Object类
|
|
|
* objectName示例:download/file.xsl,()这里是oss文件列表中 文件路径和文件名
|
|
|
*/
|
|
|
- public static OSSObject downloadOssFile(OutputStream os, String objectName) {
|
|
|
+ public static OSSObject downloadOssFile(OutputStream os, String objectName) {
|
|
|
// ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
|
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
OSSObject ossObject = ossClient.getObject(bucketName, objectName);
|