线上环境新建文件BUG修复

This commit is contained in:
wangyan21 2024-08-27 16:41:48 +08:00
parent 2a1c41ae98
commit cb30e91944

View File

@ -45,6 +45,8 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.ClassUtils;
import org.springframework.web.bind.annotation.*;
@ -81,6 +83,9 @@ public class FileController {
AsyncTaskComp asyncTaskComp;
@Autowired
private ElasticsearchClient elasticsearchClient;
@Autowired
private ResourceLoader resourceLoader;
@Value("${ufop.storage-type}")
private Integer storageType;
@ -125,15 +130,19 @@ public class FileController {
// System.out.println("ClassUtils.getDefaultClassLoader() = " + ClassUtils.getDefaultClassLoader().getResource("st"));
// System.out.println("ClassUtils.getDefaultClassLoader().getResource(\"static\") = " + ClassUtils.getDefaultClassLoader().getResource("static"));
String separatorInfo = File.separator;
String url2 = getMubanFile("static" + separatorInfo + templateFilePath.replace("/", separatorInfo));
//Spring提供了ResourceLoader接口它可以用来加载资源
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(resourceLoader);
org.springframework.core.io.Resource resource = resolver.getResource("classpath:static/" + templateFilePath.replace("/", separatorInfo));
String url2 = resource.getURL().getPath();
LOGGER.info("--->>模板文件所在的路径是:{}", url2);
System.out.println("url2 = " + url2);
url2 = URLDecoder.decode(url2, "UTF-8");
FileInputStream fileInputStream = new FileInputStream(url2);
// FileInputStream fileInputStream = new FileInputStream(url2);
Copier copier = ufopFactory.getCopier();
CopyFile copyFile = new CopyFile();
copyFile.setExtendName(extendName);
String fileUrl = copier.copy(fileInputStream, copyFile);
//直接获取resource中的输入流通过new FileInputStream(url2)会报 No such file or directory
String fileUrl = copier.copy(resource.getInputStream(), copyFile);
FileBean fileBean = new FileBean();
fileBean.setFileId(IdUtil.getSnowflakeNextIdStr());
@ -167,18 +176,6 @@ public class FileController {
}
}
/**
* user.dir默认会定位到你的工程根目录但是web项目不行如果是web项目会定位到tomcat的bin目录
*
* @param fileName
*/
public String getMubanFile(String fileName) {
String rootPath = System.getProperty("user.dir");
String fenge = File.separator;
//找寻这个路径下边的 /src/main/resources/ Windows跟Linux环境分割符号不同
return rootPath + fenge + "src" + fenge + "main" + fenge + "resources" + fenge + fileName;
}
@Operation(summary = "创建文件夹", description = "目录(文件夹)的创建", tags = {"file"})
@RequestMapping(value = "/createFold", method = RequestMethod.POST)
@MyLog(operation = "创建文件夹", module = CURRENT_MODULE)