From cc1d90965bf4c703355001152d21472b817a94a2 Mon Sep 17 00:00:00 2001 From: wangyan21 Date: Tue, 27 Aug 2024 14:06:32 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BA=BF=E4=B8=8A=E7=8E=AF=E5=A2=83=E6=96=B0?= =?UTF-8?q?=E5=BB=BA=E6=96=87=E4=BB=B6=E6=8E=A5=E5=8F=A3=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../file/controller/FileController.java | 25 ++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/qiwenshare/file/controller/FileController.java b/src/main/java/com/qiwenshare/file/controller/FileController.java index bcea618..d2aed5a 100644 --- a/src/main/java/com/qiwenshare/file/controller/FileController.java +++ b/src/main/java/com/qiwenshare/file/controller/FileController.java @@ -41,6 +41,8 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.lang3.StringUtils; import org.eclipse.jetty.util.StringUtil; +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.util.ClassUtils; @@ -49,6 +51,7 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import javax.validation.Valid; import java.io.ByteArrayInputStream; +import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.URLDecoder; @@ -64,6 +67,7 @@ import java.util.stream.Stream; @Slf4j @RequestMapping("/file") public class FileController { + private static final Logger LOGGER = LoggerFactory.getLogger(FileController.class); @Resource IFileService fileService; @@ -117,9 +121,12 @@ public class FileController { } else if ("drawio".equals(extendName)) { templateFilePath = "template/Drawio.drawio"; } - String url2 = ClassUtils.getDefaultClassLoader().getResource("static/" + templateFilePath).getPath(); - System.out.println("ClassUtils.getDefaultClassLoader() = " + ClassUtils.getDefaultClassLoader().getResource("st")); - System.out.println("ClassUtils.getDefaultClassLoader().getResource(\"static\") = " + ClassUtils.getDefaultClassLoader().getResource("static")); +// String url2 = ClassUtils.getDefaultClassLoader().getResource("static/" + templateFilePath).getPath(); +// 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)); + LOGGER.info("--->>模板文件所在的路径是:{}", url2); System.out.println("url2 = " + url2); url2 = URLDecoder.decode(url2, "UTF-8"); FileInputStream fileInputStream = new FileInputStream(url2); @@ -160,6 +167,18 @@ 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)