perf:文件路径生成调整

This commit is contained in:
曾文豪
2023-02-07 09:32:24 +08:00
parent ee3748c361
commit 92e1c1d9ef
2 changed files with 28 additions and 38 deletions

View File

@@ -66,7 +66,7 @@ public class TestController {
@RequestMapping("/uploadPath")
@TokenIgnore
public ApiResp<String> uploadPath() {
FileUploadPath uploadPath = FileUploadPath.get("http://scv6.tmp.kepai365.ltd/upload/2023-01/2b4b6b7b-70d0-4683-859a-f799adc4f04c.xls");
FileUploadPath uploadPath = FileUploadPath.file("http://scv6.tmp.kepai365.ltd/upload/2023-01/2b4b6b7b-70d0-4683-859a-f799adc4f04c.xls");
return ApiResp.respOK(uploadPath.getAbsolutePath());
}

View File

@@ -2,6 +2,7 @@ package com.tiesheng.util.pojos;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.CharUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
@@ -29,19 +30,26 @@ public class FileUploadPath {
* @param fileName
* @return
*/
public static FileUploadPath get(String fileName) {
public static FileUploadPath file(String fileName) {
// 下载文件
fileName = downloadFile(fileName);
fileName = fileDownload(fileName);
FileUploadPath pathBean = new FileUploadPath();
String tempPath = fileName;
if (!StrUtil.startWith(tempPath, CharUtil.SLASH)) {
tempPath = CharUtil.SLASH + tempPath;
}
if (!StrUtil.startWith(tempPath, UPLOAD_FOLDER)) {
tempPath = UPLOAD_FOLDER + DateUtil.format(DateUtil.date(), "yyyy-MM") + "/" + fileName;
tempPath = UPLOAD_FOLDER + DateUtil.format(DateUtil.date(), "yyyy-MM") + CharUtil.SLASH + fileName;
}
pathBean.setHttpPath(tempPath);
pathBean.setAbsolutePath(String.format("%s/static%s", System.getProperty("user.dir"), tempPath));
String tempAbs = String.format("%s/static%s", System.getProperty("user.dir"), tempPath);
tempAbs = FileUtil.normalize(tempAbs);
pathBean.setAbsolutePath(tempAbs);
FileUtil.mkParentDirs(pathBean.getAbsolutePath());
return pathBean;
@@ -51,40 +59,33 @@ public class FileUploadPath {
/**
* 下载http文件
*
* @param fileName
* @param httpUrl
* @return
*/
public static String downloadFile(String fileName) {
if (!StrUtil.startWith(fileName, "http")) {
return fileName;
public static String fileDownload(String httpUrl) {
if (!StrUtil.startWith(httpUrl, "http")) {
return httpUrl;
}
String newFileName = UPLOAD_FOLDER + StrUtil.subAfter(fileName, UPLOAD_FOLDER, true);
FileUploadPath uploadPath = get(newFileName);
String newFileName = UPLOAD_FOLDER + StrUtil.subAfter(httpUrl, UPLOAD_FOLDER, true);
FileUploadPath uploadPath = file(newFileName);
if (!FileUtil.exist(uploadPath.getAbsolutePath())) {
HttpUtil.downloadFile(fileName, uploadPath.getAbsolutePath());
HttpUtil.downloadFile(httpUrl, uploadPath.getAbsolutePath());
}
return uploadPath.getHttpPath();
}
/**
* 随机生成一个文件路径
*
* @return
*/
public static FileUploadPath random() {
return get(IdUtil.simpleUUID());
}
/**
* 随机生成一个文件路径
*
* @return
*/
public static FileUploadPath random(String fileExt) {
return get(IdUtil.simpleUUID() + "." + fileExt);
String fileName = IdUtil.simpleUUID();
if (!StrUtil.isEmpty(fileExt)) {
fileName = fileName + "." + fileExt;
}
return file(fileName);
}
@@ -95,20 +96,9 @@ public class FileUploadPath {
* @return
*/
public static FileUploadPath folder(String folder) {
FileUploadPath pathBean = new FileUploadPath();
String tempFolder = folder;
if (!StrUtil.startWith(tempFolder, UPLOAD_FOLDER)) {
tempFolder = UPLOAD_FOLDER + tempFolder;
}
if (!StrUtil.endWith(folder, "/")) {
tempFolder = tempFolder + "/";
}
pathBean.setHttpPath(tempFolder);
pathBean.setAbsolutePath(String.format("%s/static%s", System.getProperty("user.dir"), tempFolder));
FileUtil.mkParentDirs(pathBean.getAbsolutePath());
return pathBean;
FileUploadPath file = file(folder);
FileUtil.mkdir(file.getAbsolutePath());
return file;
}