|
@@ -1,28 +1,31 @@
|
|
|
package com.sckw.file.utils;
|
|
package com.sckw.file.utils;
|
|
|
|
|
|
|
|
import cn.hutool.core.date.DateTime;
|
|
import cn.hutool.core.date.DateTime;
|
|
|
-import com.aliyun.oss.ClientException;
|
|
|
|
|
-import com.aliyun.oss.OSS;
|
|
|
|
|
-import com.aliyun.oss.OSSClientBuilder;
|
|
|
|
|
-import com.aliyun.oss.OSSException;
|
|
|
|
|
|
|
+import com.aliyun.oss.*;
|
|
|
import com.aliyun.oss.model.*;
|
|
import com.aliyun.oss.model.*;
|
|
|
import com.sckw.core.utils.IdWorker;
|
|
import com.sckw.core.utils.IdWorker;
|
|
|
|
|
+import com.sckw.core.utils.PasswordUtils;
|
|
|
import com.sckw.core.utils.StringUtils;
|
|
import com.sckw.core.utils.StringUtils;
|
|
|
|
|
+import com.sckw.excel.common.NumberConstant;
|
|
|
import com.sckw.file.common.enums.AliyunOssFileTypeEnum;
|
|
import com.sckw.file.common.enums.AliyunOssFileTypeEnum;
|
|
|
import com.sckw.file.common.enums.FileEnum;
|
|
import com.sckw.file.common.enums.FileEnum;
|
|
|
import jakarta.annotation.PostConstruct;
|
|
import jakarta.annotation.PostConstruct;
|
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
import jakarta.servlet.http.HttpServletResponse;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
+import org.apache.commons.io.FilenameUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.context.annotation.Configuration;
|
|
import org.springframework.context.annotation.Configuration;
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
|
|
import java.io.*;
|
|
import java.io.*;
|
|
|
|
|
+import java.math.BigDecimal;
|
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
|
import java.text.DecimalFormat;
|
|
import java.text.DecimalFormat;
|
|
|
import java.util.Date;
|
|
import java.util.Date;
|
|
|
|
|
+import java.util.HashMap;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* @author lfdc
|
|
* @author lfdc
|
|
@@ -86,6 +89,69 @@ public class FileUtils {
|
|
|
bucketName = this.oss_bucketName;
|
|
bucketName = this.oss_bucketName;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取指定文件或文件夹的后缀名
|
|
|
|
|
+ * @param fileName 文件名称
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String getFileSuffix(String fileName) {
|
|
|
|
|
+ return fileName.substring(fileName.lastIndexOf("."));//例如:abc.png 截取后:.png
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 文件上传
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param file 文件
|
|
|
|
|
+ * @param fileEnum 上传使用的枚举
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static Map<String, String> uploadFileByInfo(MultipartFile file, FileEnum fileEnum) {
|
|
|
|
|
+ Map<String, String> infoMap = new HashMap<>();
|
|
|
|
|
+ try {
|
|
|
|
|
+ //创建OSSClient实例
|
|
|
|
|
+ defalutOSS();
|
|
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
|
|
+ //容器不存在,就创建
|
|
|
|
|
+ if (!ossClient.doesBucketExist(bucketName)) {
|
|
|
|
|
+ ossClient.createBucket(bucketName);
|
|
|
|
|
+ CreateBucketRequest createBucketRequest = new CreateBucketRequest(bucketName);
|
|
|
|
|
+ createBucketRequest.setCannedACL(CannedAccessControlList.PublicRead);
|
|
|
|
|
+ ossClient.createBucket(createBucketRequest);
|
|
|
|
|
+ }
|
|
|
|
|
+ //上传文件流
|
|
|
|
|
+ InputStream inputStream = file.getInputStream();
|
|
|
|
|
+ String fileName = FilenameUtils.getBaseName(file.getOriginalFilename());
|
|
|
|
|
+ //生成随机唯一值,使用uuid,添加到文件名称里面 改成使用加密
|
|
|
|
|
+// fileName = PasswordUtils.md5(fileName);
|
|
|
|
|
+ fileName = PasswordUtils.md5(fileName);
|
|
|
|
|
+ String fileSuffix = FileUtils.getFileSuffix(file.getOriginalFilename());
|
|
|
|
|
+ //不带后缀
|
|
|
|
|
+ infoMap.put("fileMd5", fileName);
|
|
|
|
|
+ infoMap.put("fileName", fileName + fileSuffix);
|
|
|
|
|
+ //按照当前日期,创建文件夹,上传到创建文件夹里面
|
|
|
|
|
+ //2021/02/02/01.jpg
|
|
|
|
|
+ String timeUrl = new DateTime().toString("yyyyMMdd");
|
|
|
|
|
+ fileName = timeUrl + "/" + fileName;
|
|
|
|
|
+ String filePath = BASE_DIR + fileName+fileSuffix;
|
|
|
|
|
+ //调用方法实现上传
|
|
|
|
|
+ ossClient.putObject(bucketName, filePath, inputStream);
|
|
|
|
|
+ //上传后的文件地址
|
|
|
|
|
+// String url1 = getUrl(ossClient, bucketName, filePath);
|
|
|
|
|
+ //关闭OSSClient。
|
|
|
|
|
+ ossClient.shutdown();
|
|
|
|
|
+ //上传之后文件路径
|
|
|
|
|
+ //https://yygh-atguigu.oss-cn-beijing.aliyuncs.com/01.jpg
|
|
|
|
|
+ String url = "https://" + bucketName + "." + endpoint + "/" + filePath;
|
|
|
|
|
+ //返回 上传文件地址
|
|
|
|
|
+ infoMap.put("filePath", url);
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ log.error("oss-upload-file-error:{}", e.getMessage(), e);
|
|
|
|
|
+ throw new RuntimeException("oss-upload-file-error:"+e.getMessage());
|
|
|
|
|
+ }
|
|
|
|
|
+ return infoMap;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 上传文件
|
|
* 上传文件
|
|
@@ -114,7 +180,8 @@ public class FileUtils {
|
|
|
String fileName = file.getOriginalFilename();
|
|
String fileName = file.getOriginalFilename();
|
|
|
//生成随机唯一值,使用uuid,添加到文件名称里面
|
|
//生成随机唯一值,使用uuid,添加到文件名称里面
|
|
|
long uuid = new IdWorker(1).nextId();
|
|
long uuid = new IdWorker(1).nextId();
|
|
|
- fileName = String.valueOf(uuid) + fileName;
|
|
|
|
|
|
|
+// fileName = String.valueOf(uuid) + fileName;
|
|
|
|
|
+ fileName = PasswordUtils.md5(fileName);
|
|
|
//按照当前日期,创建文件夹,上传到创建文件夹里面
|
|
//按照当前日期,创建文件夹,上传到创建文件夹里面
|
|
|
//2021/02/02/01.jpgossClient = {OSSClient@13049}
|
|
//2021/02/02/01.jpgossClient = {OSSClient@13049}
|
|
|
String timeUrl = new DateTime().toString("yyyyMMdd");
|
|
String timeUrl = new DateTime().toString("yyyyMMdd");
|
|
@@ -177,6 +244,35 @@ public class FileUtils {
|
|
|
return fileSizeString;
|
|
return fileSizeString;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取文件上传大小
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param file
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static BigDecimal getFileSize(MultipartFile file, String type) {
|
|
|
|
|
+ long size = file.getSize();
|
|
|
|
|
+// DecimalFormat df = new DecimalFormat("#.00");
|
|
|
|
|
+ BigDecimal bigDecimal = new BigDecimal(NumberConstant.ZERO);
|
|
|
|
|
+ switch (type) {
|
|
|
|
|
+ case "B":
|
|
|
|
|
+ bigDecimal = new BigDecimal((double) size);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case "KB":
|
|
|
|
|
+ bigDecimal = new BigDecimal((double) size / 1024);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case "MB":
|
|
|
|
|
+ bigDecimal = new BigDecimal((double) size / 1048576);
|
|
|
|
|
+ break;
|
|
|
|
|
+ case "GB":
|
|
|
|
|
+ bigDecimal = new BigDecimal((double) size / 1073741824);
|
|
|
|
|
+ break;
|
|
|
|
|
+ default:
|
|
|
|
|
+ throw new RuntimeException("file size error");
|
|
|
|
|
+ }
|
|
|
|
|
+ return bigDecimal;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 获得阿里云OSS客户端对象
|
|
* 获得阿里云OSS客户端对象
|
|
|
*
|
|
*
|
|
@@ -257,7 +353,8 @@ public class FileUtils {
|
|
|
* 通过文件名字下载
|
|
* 通过文件名字下载
|
|
|
*
|
|
*
|
|
|
* @param response response
|
|
* @param response response
|
|
|
- * @param fileName 文件名字,带后缀,例子:postman.txt 文件全路径 https://kaiwu-saas.oss-cn-chengdu.aliyuncs.com/kll/uploads/20230605/146325493677821952598454132.txt
|
|
|
|
|
|
|
+ * @param fileName 文件名字,带后缀,例子:postman.txt
|
|
|
|
|
+ * 文件全路径 https://kaiwu-saas.oss-cn-chengdu.aliyuncs.com/kll/uploads/20230605/146325493677821952598454132.txt
|
|
|
*/
|
|
*/
|
|
|
public static void downloadByFileName(HttpServletResponse response, String fileName) {
|
|
public static void downloadByFileName(HttpServletResponse response, String fileName) {
|
|
|
defalutOSS();
|
|
defalutOSS();
|
|
@@ -266,7 +363,7 @@ public class FileUtils {
|
|
|
String contentType = ossObject.getObjectMetadata().getContentType();
|
|
String contentType = ossObject.getObjectMetadata().getContentType();
|
|
|
String contentType1 = getContentType(fileName);
|
|
String contentType1 = getContentType(fileName);
|
|
|
System.out.println("contentType:" + contentType);
|
|
System.out.println("contentType:" + contentType);
|
|
|
- System.out.println("contentType1:" + contentType1);
|
|
|
|
|
|
|
+// System.out.println("contentType1:" + contentType1);
|
|
|
//设置响应内容类型,当设置了ContentType为“image/jpg”时,浏览器可以直接显示图片;
|
|
//设置响应内容类型,当设置了ContentType为“image/jpg”时,浏览器可以直接显示图片;
|
|
|
response.setContentType(contentType);
|
|
response.setContentType(contentType);
|
|
|
BufferedInputStream in = new BufferedInputStream(ossObject.getObjectContent());
|
|
BufferedInputStream in = new BufferedInputStream(ossObject.getObjectContent());
|
|
@@ -466,7 +563,7 @@ public class FileUtils {
|
|
|
*/
|
|
*/
|
|
|
public static String getUrl(OSS ossClient, String bucketName, String key) {
|
|
public static String getUrl(OSS ossClient, String bucketName, String key) {
|
|
|
//设置URl过期时间为99年:3600L*1000*24*365*99
|
|
//设置URl过期时间为99年:3600L*1000*24*365*99
|
|
|
- Date expiration = new Date(new Date().getTime() + 3600l * 1000 * 24 * 365 * 99);
|
|
|
|
|
|
|
+ Date expiration = new Date( System.currentTimeMillis()+ 3600L * 1000 * 24 * 365 * 99);
|
|
|
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, key);
|
|
GeneratePresignedUrlRequest generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, key);
|
|
|
generatePresignedUrlRequest.setExpiration(expiration);
|
|
generatePresignedUrlRequest.setExpiration(expiration);
|
|
|
URL url = ossClient.generatePresignedUrl(generatePresignedUrlRequest);
|
|
URL url = ossClient.generatePresignedUrl(generatePresignedUrlRequest);
|
|
@@ -568,6 +665,40 @@ public class FileUtils {
|
|
|
downloadByFileName(response, url);
|
|
downloadByFileName(response, url);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * @Title:downloadToFile
|
|
|
|
|
+ * @Description: 下载文件到本地
|
|
|
|
|
+ * @param @param ossFilePath oss上的文件路径(全路径)
|
|
|
|
|
+ * @param @param newFilePath 本地文件路径 (全路径)
|
|
|
|
|
+ * @return void 返回类型
|
|
|
|
|
+ */
|
|
|
|
|
+ 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 {
|
|
|
|
|
+ // 指定路径如果没有则创建并添加
|
|
|
|
|
+ File file = new File(newFilePath);
|
|
|
|
|
+ //获取父目录
|
|
|
|
|
+ File fileParent = file.getParentFile();
|
|
|
|
|
+ //判断是否存在
|
|
|
|
|
+ if (!fileParent.exists()) {
|
|
|
|
|
+ // 创建父目录文件
|
|
|
|
|
+ fileParent.mkdirs();
|
|
|
|
|
+ }
|
|
|
|
|
+ file.createNewFile();
|
|
|
|
|
+ // 下载Object到本地文件,并保存到指定的本地路径中。如果指定的本地文件存在会覆盖,不存在则新建。
|
|
|
|
|
+ // 如果未指定本地路径,则下载后的文件默认保存到示例程序所属项目对应本地路径中。
|
|
|
|
|
+ ossClient.getObject(new GetObjectRequest(bucketName, ossFilePath), new File(newFilePath));
|
|
|
|
|
+
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ if (ossClient != null) {
|
|
|
|
|
+ ossClient.shutdown();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* 判断文件是否存在
|
|
* 判断文件是否存在
|
|
|
*
|
|
*
|
|
@@ -602,6 +733,116 @@ public class FileUtils {
|
|
|
return split[split.length - 1];
|
|
return split[split.length - 1];
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 字符串转成file文件
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param text
|
|
|
|
|
+ * @param file
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ public static File stringByFile(String text, File file) {
|
|
|
|
|
+ if (file.exists()) {
|
|
|
|
|
+ file.delete();
|
|
|
|
|
+ }
|
|
|
|
|
+ BufferedReader br = null;
|
|
|
|
|
+ BufferedWriter bw = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ br = new BufferedReader(new StringReader(text));
|
|
|
|
|
+ bw = new BufferedWriter(new FileWriter(file));
|
|
|
|
|
+ char[] buf = new char[1024 * 64]; //字符缓冲区
|
|
|
|
|
+ int len;
|
|
|
|
|
+ while ((len = br.read(buf)) != -1) {
|
|
|
|
|
+ bw.write(buf, 0, len);
|
|
|
|
|
+ }
|
|
|
|
|
+ bw.flush();
|
|
|
|
|
+ br.close();
|
|
|
|
|
+ bw.close();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ return file;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * file转byte
|
|
|
|
|
+ */
|
|
|
|
|
+ public static byte[] file2byte(File file) {
|
|
|
|
|
+ byte[] buffer = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ FileInputStream fis = new FileInputStream(file);
|
|
|
|
|
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|
|
|
|
+ byte[] b = new byte[1024];
|
|
|
|
|
+ int n;
|
|
|
|
|
+ while ((n = fis.read(b)) != -1) {
|
|
|
|
|
+ bos.write(b, 0, n);
|
|
|
|
|
+ }
|
|
|
|
|
+ fis.close();
|
|
|
|
|
+ bos.close();
|
|
|
|
|
+ buffer = bos.toByteArray();
|
|
|
|
|
+ } catch (FileNotFoundException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ return buffer;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * byte 转file
|
|
|
|
|
+ */
|
|
|
|
|
+ public static File byte2File(byte[] buf, String filePath, String fileName) {
|
|
|
|
|
+ BufferedOutputStream bos = null;
|
|
|
|
|
+ FileOutputStream fos = null;
|
|
|
|
|
+ File file = null;
|
|
|
|
|
+ try {
|
|
|
|
|
+ File dir = new File(filePath);
|
|
|
|
|
+ if (!dir.exists() && dir.isDirectory()) {
|
|
|
|
|
+ dir.mkdirs();
|
|
|
|
|
+ }
|
|
|
|
|
+ file = new File(filePath + File.separator + fileName);
|
|
|
|
|
+ fos = new FileOutputStream(file);
|
|
|
|
|
+ bos = new BufferedOutputStream(fos);
|
|
|
|
|
+ bos.write(buf);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ } finally {
|
|
|
|
|
+ if (bos != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ bos.close();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ if (fos != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ fos.close();
|
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return file;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * multipartFile转File
|
|
|
|
|
+ **/
|
|
|
|
|
+ public static File multipartFile2File(MultipartFile multipartFile) {
|
|
|
|
|
+ File file = null;
|
|
|
|
|
+ if (multipartFile != null) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ file = File.createTempFile("tmp", null);
|
|
|
|
|
+ multipartFile.transferTo(file);
|
|
|
|
|
+ System.gc();
|
|
|
|
|
+ file.deleteOnExit();
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ e.printStackTrace();
|
|
|
|
|
+ log.warn("multipartFile转File发生异常:" + e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+ return file;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
|
|
|
// 测试
|
|
// 测试
|
|
|
public static void main(String[] args) throws FileNotFoundException {
|
|
public static void main(String[] args) throws FileNotFoundException {
|