perf:FileUploadPath兼容http地址

This commit is contained in:
曾文豪
2023-01-11 14:40:27 +08:00
parent a585a68194
commit c6ee1f2279
2 changed files with 33 additions and 0 deletions

View File

@@ -6,6 +6,7 @@ import com.tiesheng.message.config.aliyun.AliyunSmsConfig;
import com.tiesheng.message.pojos.MessageReqResp; import com.tiesheng.message.pojos.MessageReqResp;
import com.tiesheng.util.config.GlobalConfig; import com.tiesheng.util.config.GlobalConfig;
import com.tiesheng.util.pojos.ApiResp; import com.tiesheng.util.pojos.ApiResp;
import com.tiesheng.util.pojos.FileUploadPath;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@@ -62,4 +63,11 @@ public class TestController {
return ApiResp.respOK(""); return ApiResp.respOK("");
} }
@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");
return ApiResp.respOK(uploadPath.getAbsolutePath());
}
} }

View File

@@ -4,6 +4,7 @@ import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil; import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil; import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
public class FileUploadPath { public class FileUploadPath {
@@ -29,6 +30,10 @@ public class FileUploadPath {
* @return * @return
*/ */
public static FileUploadPath get(String fileName) { public static FileUploadPath get(String fileName) {
// 下载文件
fileName = downloadFile(fileName);
FileUploadPath pathBean = new FileUploadPath(); FileUploadPath pathBean = new FileUploadPath();
String tempPath = fileName; String tempPath = fileName;
@@ -43,6 +48,26 @@ public class FileUploadPath {
} }
/**
* 下载http文件
*
* @param fileName
* @return
*/
public static String downloadFile(String fileName) {
if (!StrUtil.startWith(fileName, "http")) {
return fileName;
}
String newFileName = UPLOAD_FOLDER + StrUtil.subAfter(fileName, UPLOAD_FOLDER, true);
FileUploadPath uploadPath = get(newFileName);
if (!FileUtil.exist(uploadPath.getAbsolutePath())) {
HttpUtil.downloadFile(fileName, uploadPath.getAbsolutePath());
}
return uploadPath.getHttpPath();
}
/** /**
* 随机生成一个文件路径 * 随机生成一个文件路径
* *