|
@@ -91,6 +91,7 @@ public class FileUtils {
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 获取指定文件或文件夹的后缀名
|
|
* 获取指定文件或文件夹的后缀名
|
|
|
|
|
+ *
|
|
|
* @param fileName 文件名称
|
|
* @param fileName 文件名称
|
|
|
* @return
|
|
* @return
|
|
|
*/
|
|
*/
|
|
@@ -108,10 +109,10 @@ public class FileUtils {
|
|
|
*/
|
|
*/
|
|
|
public static Map<String, String> uploadFileByInfo(MultipartFile file, FileEnum fileEnum) {
|
|
public static Map<String, String> uploadFileByInfo(MultipartFile file, FileEnum fileEnum) {
|
|
|
Map<String, String> infoMap = new HashMap<>();
|
|
Map<String, String> infoMap = new HashMap<>();
|
|
|
|
|
+ //创建OSSClient实例
|
|
|
|
|
+ defalutOSS();
|
|
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
try {
|
|
try {
|
|
|
- //创建OSSClient实例
|
|
|
|
|
- defalutOSS();
|
|
|
|
|
- OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
|
|
//容器不存在,就创建
|
|
//容器不存在,就创建
|
|
|
if (!ossClient.doesBucketExist(bucketName)) {
|
|
if (!ossClient.doesBucketExist(bucketName)) {
|
|
|
ossClient.createBucket(bucketName);
|
|
ossClient.createBucket(bucketName);
|
|
@@ -133,7 +134,7 @@ public class FileUtils {
|
|
|
//2021/02/02/01.jpg
|
|
//2021/02/02/01.jpg
|
|
|
String timeUrl = new DateTime().toString("yyyyMMdd");
|
|
String timeUrl = new DateTime().toString("yyyyMMdd");
|
|
|
fileName = timeUrl + "/" + fileName;
|
|
fileName = timeUrl + "/" + fileName;
|
|
|
- String filePath = BASE_DIR + fileName+fileSuffix;
|
|
|
|
|
|
|
+ String filePath = BASE_DIR + fileName + fileSuffix;
|
|
|
//调用方法实现上传
|
|
//调用方法实现上传
|
|
|
ossClient.putObject(bucketName, filePath, inputStream);
|
|
ossClient.putObject(bucketName, filePath, inputStream);
|
|
|
//上传后的文件地址
|
|
//上传后的文件地址
|
|
@@ -142,13 +143,20 @@ public class FileUtils {
|
|
|
ossClient.shutdown();
|
|
ossClient.shutdown();
|
|
|
//上传之后文件路径
|
|
//上传之后文件路径
|
|
|
//https://yygh-atguigu.oss-cn-beijing.aliyuncs.com/01.jpg
|
|
//https://yygh-atguigu.oss-cn-beijing.aliyuncs.com/01.jpg
|
|
|
- String url = "https://" + bucketName + "." + endpoint + "/" + filePath;
|
|
|
|
|
|
|
+// String url = "https://" + bucketName + "." + endpoint + "/" + 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);
|
|
infoMap.put("filePath", url);
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
|
|
|
+ infoMap.put("filePath", null);
|
|
|
|
|
+ //关闭OSSClient。
|
|
|
|
|
+ ossClient.shutdown();
|
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
|
log.error("oss-upload-file-error:{}", e.getMessage(), e);
|
|
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;
|
|
return infoMap;
|
|
|
}
|
|
}
|
|
@@ -163,6 +171,9 @@ public class FileUtils {
|
|
|
* @return
|
|
* @return
|
|
|
*/
|
|
*/
|
|
|
public static String uploadFile(MultipartFile file, FileEnum fileEnum) {
|
|
public static String uploadFile(MultipartFile file, FileEnum fileEnum) {
|
|
|
|
|
+ // 私有云要关闭CNAME
|
|
|
|
|
+ ClientBuilderConfiguration conf = new ClientBuilderConfiguration();
|
|
|
|
|
+ conf.setSupportCname(false);
|
|
|
try {
|
|
try {
|
|
|
//创建OSSClient实例
|
|
//创建OSSClient实例
|
|
|
defalutOSS();
|
|
defalutOSS();
|
|
@@ -187,8 +198,11 @@ public class FileUtils {
|
|
|
String timeUrl = new DateTime().toString("yyyyMMdd");
|
|
String timeUrl = new DateTime().toString("yyyyMMdd");
|
|
|
fileName = timeUrl + "/" + fileName;
|
|
fileName = timeUrl + "/" + fileName;
|
|
|
String filePath = BASE_DIR + 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);
|
|
// String url1 = getUrl(ossClient, bucketName, filePath);
|
|
|
// System.out.println(url1);
|
|
// System.out.println(url1);
|
|
@@ -252,7 +266,6 @@ public class FileUtils {
|
|
|
*/
|
|
*/
|
|
|
public static BigDecimal getFileSize(MultipartFile file, String type) {
|
|
public static BigDecimal getFileSize(MultipartFile file, String type) {
|
|
|
long size = file.getSize();
|
|
long size = file.getSize();
|
|
|
-// DecimalFormat df = new DecimalFormat("#.00");
|
|
|
|
|
BigDecimal bigDecimal = new BigDecimal(NumberConstant.ZERO);
|
|
BigDecimal bigDecimal = new BigDecimal(NumberConstant.ZERO);
|
|
|
switch (type) {
|
|
switch (type) {
|
|
|
case "B":
|
|
case "B":
|
|
@@ -307,6 +320,11 @@ public class FileUtils {
|
|
|
return AliyunOssFileTypeEnum.TXT.getText();
|
|
return AliyunOssFileTypeEnum.TXT.getText();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param fileName
|
|
|
|
|
+ * @param response
|
|
|
|
|
+ */
|
|
|
public static void downOSSFile(String fileName, HttpServletResponse response) {
|
|
public static void downOSSFile(String fileName, HttpServletResponse response) {
|
|
|
BufferedInputStream input = null;
|
|
BufferedInputStream input = null;
|
|
|
OutputStream outputStream = null;
|
|
OutputStream outputStream = null;
|
|
@@ -354,15 +372,19 @@ public class FileUtils {
|
|
|
*
|
|
*
|
|
|
* @param response response
|
|
* @param response response
|
|
|
* @param fileName 文件名字,带后缀,例子:postman.txt
|
|
* @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) {
|
|
public static void downloadByFileName(HttpServletResponse response, String fileName) {
|
|
|
defalutOSS();
|
|
defalutOSS();
|
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
OSSObject ossObject = ossClient.getObject(bucketName, fileName);
|
|
OSSObject ossObject = ossClient.getObject(bucketName, fileName);
|
|
|
|
|
+ boolean exist = ossClient.doesObjectExist(bucketName, fileName);
|
|
|
|
|
+ if (!exist) {
|
|
|
|
|
+ throw new RuntimeException("下载文件不存在!");
|
|
|
|
|
+ }
|
|
|
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);
|
|
@@ -370,7 +392,8 @@ public class FileUtils {
|
|
|
try {
|
|
try {
|
|
|
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
|
|
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
|
|
|
//通知浏览器以附件形式下载
|
|
//通知浏览器以附件形式下载
|
|
|
- response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8"));
|
|
|
|
|
|
|
+// fileName = URLEncoder.encode(fileName, "UTF-8").replaceAll("\\+", "%20");
|
|
|
|
|
+ response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "utf-8").replaceAll("\\+", "%20"));
|
|
|
byte[] car = new byte[1024];
|
|
byte[] car = new byte[1024];
|
|
|
int len = 0;
|
|
int len = 0;
|
|
|
while ((len = in.read(car)) != -1) {
|
|
while ((len = in.read(car)) != -1) {
|
|
@@ -563,7 +586,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( 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 generatePresignedUrlRequest = new GeneratePresignedUrlRequest(bucketName, key);
|
|
|
generatePresignedUrlRequest.setExpiration(expiration);
|
|
generatePresignedUrlRequest.setExpiration(expiration);
|
|
|
URL url = ossClient.generatePresignedUrl(generatePresignedUrlRequest);
|
|
URL url = ossClient.generatePresignedUrl(generatePresignedUrlRequest);
|
|
@@ -593,7 +616,9 @@ public class FileUtils {
|
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent()));
|
|
BufferedReader reader = new BufferedReader(new InputStreamReader(ossObject.getObjectContent()));
|
|
|
while (true) {
|
|
while (true) {
|
|
|
String line = reader.readLine();
|
|
String line = reader.readLine();
|
|
|
- if (line == null) break;
|
|
|
|
|
|
|
+ if (line == null) {
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
System.out.println("\n" + line);
|
|
System.out.println("\n" + line);
|
|
|
}
|
|
}
|
|
@@ -659,20 +684,20 @@ public class FileUtils {
|
|
|
* 通过oss的完整key下载
|
|
* 通过oss的完整key下载
|
|
|
*
|
|
*
|
|
|
* @param response response
|
|
* @param response response
|
|
|
- * @param url 全路径的url地址
|
|
|
|
|
|
|
+ * @param url 全路径的url地址 kll/uploads/20230621/07ccbec381a011d121a215719199ac49.html
|
|
|
*/
|
|
*/
|
|
|
public static void downloadByUrl(HttpServletResponse response, String url) {
|
|
public static void downloadByUrl(HttpServletResponse response, String url) {
|
|
|
downloadByFileName(response, url);
|
|
downloadByFileName(response, url);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
- * @Title:downloadToFile
|
|
|
|
|
- * @Description: 下载文件到本地
|
|
|
|
|
* @param @param ossFilePath oss上的文件路径(全路径)
|
|
* @param @param ossFilePath oss上的文件路径(全路径)
|
|
|
* @param @param newFilePath 本地文件路径 (全路径)
|
|
* @param @param newFilePath 本地文件路径 (全路径)
|
|
|
* @return void 返回类型
|
|
* @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 = (OSSClient) new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
|
|
OSSClient ossClient = (OSSClient) new OSSClientBuilder().build(endPoint, accessKeyId, accessKeySecret);
|
|
|
try {
|
|
try {
|
|
@@ -844,6 +869,115 @@ public class FileUtils {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 删除文件
|
|
|
|
|
+ *
|
|
|
|
|
+ * @param url 示例:'download/file.xsl' oss服务器文件路径以及文件名
|
|
|
|
|
+ */
|
|
|
|
|
+ public static void remove(String url) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 创建OSSClient实例。
|
|
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
|
|
+ // 判断当前文件url 是否存在
|
|
|
|
|
+ boolean exist = ossClient.doesObjectExist(bucketName, url);
|
|
|
|
|
+ if (!exist) {
|
|
|
|
|
+ System.out.println("文件不存在");
|
|
|
|
|
+ } else {
|
|
|
|
|
+ // 删除文件。
|
|
|
|
|
+ ossClient.deleteObject(bucketName, url);
|
|
|
|
|
+ // 关闭OSSClient。
|
|
|
|
|
+ ossClient.shutdown();
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ System.out.println(e);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 获取OSS连接
|
|
|
|
|
+ *
|
|
|
|
|
+ * @return
|
|
|
|
|
+ */
|
|
|
|
|
+ private static void getOSSClient() {
|
|
|
|
|
+ if (ossclient == null) {
|
|
|
|
|
+ ossclient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 根据key删除OSS服务器上的文件
|
|
|
|
|
+ * <p>
|
|
|
|
|
+ * param ossClient oss连接
|
|
|
|
|
+ * param bucketName 存储空间
|
|
|
|
|
+ * param folder 模拟文件夹名 如"qj_nanjing/"
|
|
|
|
|
+ * param key Bucket下的文件的路径名+文件名 如:"upload/cake.jpg"
|
|
|
|
|
+ */
|
|
|
|
|
+ public static void deleteFile(String bucketName, String folder, String key) {
|
|
|
|
|
+ //获取OSS存储client
|
|
|
|
|
+ getOSSClient();
|
|
|
|
|
+ ossclient.deleteObject(bucketName, folder + key);
|
|
|
|
|
+ log.info("删除" + bucketName + "下的文件" + folder + key + "成功");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 通过文件名判断并获取OSS服务文件上传时文件的contentType
|
|
|
|
|
+ * param fileName 文件名
|
|
|
|
|
+ * return 文件的 contentType
|
|
|
|
|
+ */
|
|
|
|
|
+ public static String getContentTypeByOSS(String fileName) {
|
|
|
|
|
+ // 文件的后缀名
|
|
|
|
|
+ String fileExtension = fileName.substring(fileName.lastIndexOf("."));
|
|
|
|
|
+ if (".bmp".equalsIgnoreCase(fileExtension)) {
|
|
|
|
|
+ return "image/bmp";
|
|
|
|
|
+ }
|
|
|
|
|
+ if (".gif".equalsIgnoreCase(fileExtension)) {
|
|
|
|
|
+ return "image/gif";
|
|
|
|
|
+ }
|
|
|
|
|
+ if (".jpeg".equalsIgnoreCase(fileExtension) || ".jpg".equalsIgnoreCase(fileExtension)
|
|
|
|
|
+ || ".png".equalsIgnoreCase(fileExtension)) {
|
|
|
|
|
+ return "image/jpeg";
|
|
|
|
|
+ }
|
|
|
|
|
+ if (".png".equalsIgnoreCase(fileExtension)) {
|
|
|
|
|
+ return "image/png";
|
|
|
|
|
+ }
|
|
|
|
|
+ if (".html".equalsIgnoreCase(fileExtension)) {
|
|
|
|
|
+ return "text/html";
|
|
|
|
|
+ }
|
|
|
|
|
+ if (".txt".equalsIgnoreCase(fileExtension)) {
|
|
|
|
|
+ return "text/plain";
|
|
|
|
|
+ }
|
|
|
|
|
+ if (".vsd".equalsIgnoreCase(fileExtension)) {
|
|
|
|
|
+ return "application/vnd.visio";
|
|
|
|
|
+ }
|
|
|
|
|
+ if (".ppt".equalsIgnoreCase(fileExtension) || "pptx".equalsIgnoreCase(fileExtension)) {
|
|
|
|
|
+ return "application/vnd.ms-powerpoint";
|
|
|
|
|
+ }
|
|
|
|
|
+ if (".doc".equalsIgnoreCase(fileExtension) || "docx".equalsIgnoreCase(fileExtension)) {
|
|
|
|
|
+ return "application/msword";
|
|
|
|
|
+ }
|
|
|
|
|
+ if (".xml".equalsIgnoreCase(fileExtension)) {
|
|
|
|
|
+ return "text/xml";
|
|
|
|
|
+ }
|
|
|
|
|
+ // 默认返回类型
|
|
|
|
|
+ return null;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 下载文件,此处主要是给Controller层提供一个OSS的Object类
|
|
|
|
|
+ * objectName示例:download/file.xsl,()这里是oss文件列表中 文件路径和文件名
|
|
|
|
|
+ */
|
|
|
|
|
+ public static OSSObject downloadOssFile(OutputStream os, String objectName) {
|
|
|
|
|
+ // ossObject包含文件所在的存储空间名称、文件名称、文件元信息以及一个输入流。
|
|
|
|
|
+ OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
|
|
|
|
|
+ OSSObject ossObject = ossClient.getObject(bucketName, objectName);
|
|
|
|
|
+ return ossObject;
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
// 测试
|
|
// 测试
|
|
|
public static void main(String[] args) throws FileNotFoundException {
|
|
public static void main(String[] args) throws FileNotFoundException {
|
|
|
//阿里云OSS账号自行到阿里云官网申请
|
|
//阿里云OSS账号自行到阿里云官网申请
|