本文共 3179 字,大约阅读时间需要 10 分钟。
getServletContext().getRealPath()为临时目录
默认情况下Spring Boot中request.getServletContext().getRealPath()返回的是一个临时文件夹的地址
org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory
protected void prepareContext(Host host, ServletContextInitializer[] initializers) { File documentRoot = getValidDocumentRoot(); TomcatEmbeddedContext context = new TomcatEmbeddedContext(); if (documentRoot != null) { context.setResources(new LoaderHidingResourceRoot(context)); } context.setName(getContextPath()); context.setDisplayName(getDisplayName()); context.setPath(getContextPath()); File docBase = (documentRoot != null) ? documentRoot : createTempDir("tomcat-docbase");//关键创建临时文件夹 context.setDocBase(docBase.getAbsolutePath()); context.addLifecycleListener(new FixContextListener()); context.setParentClassLoader((this.resourceLoader != null) ? this.resourceLoader.getClassLoader() : ClassUtils.getDefaultClassLoader()); resetDefaultLocaleMapping(context); addLocaleMappings(context);
创建的临时目录位置
org.springframework.boot.web.servlet.server.AbstractServletWebServerFactory
private final DocumentRoot documentRoot = new DocumentRoot(this.logger); /** * Returns the absolute document root when it points to a valid directory, logging a * warning and returning {@code null} otherwise. * @return the valid document root */ protected final File getValidDocumentRoot() { return this.documentRoot.getValidDirectory(); }
org.springframework.boot.web.servlet.server.DocumentRoot
/** * Returns the absolute document root when it points to a valid directory, logging a * warning and returning {@code null} otherwise. * @return the valid document root */ final File getValidDirectory() { File file = this.directory; // If document root not explicitly set see if we are running from a war archive file = (file != null) ? file : getWarFileDocumentRoot(); // If not a war archive maybe it is an exploded war file = (file != null) ? file : getExplodedWarFileDocumentRoot(); // Or maybe there is a document root in a well-known location file = (file != null) ? file : getCommonDocumentRoot(); if (file == null && this.logger.isDebugEnabled()) { logNoDocumentRoots(); } else if (this.logger.isDebugEnabled()) { this.logger.debug("Document root: " + file); } return file; }
发现有三种取路径方式:
war包 getWarFileDocumentRoot
导出包 getExplodedWarFileDocumentRoot
文档 getCommonDocumentRoot
内置tomcat启动应该属于第三种。
private static final String[] COMMON_DOC_ROOTS = { "src/main/webapp", "public","static" };
private File getCommonDocumentRoot() { for (String commonDocRoot : COMMON_DOC_ROOTS) { File root = new File(commonDocRoot); if (root.exists() && root.isDirectory()) { return root.getAbsoluteFile(); } } return null; }
百度得知 取的是
System.getProperty("user.dir")
相当于
File root = new File(System.getProperty("user.dir")+"src/main/webapp");
输出 System.getProperty("user.dir") 是项目层目录
Spring Boot会尝试读取COMMON_DOC_ROOTS 配置里面的路径
在 Spring Boot 所在的jar包或者项目所在的根目录下新建一个src/main/webapp的目录、public或者static的目录
那么通过 request.getServletContext().getRealPath()就会得到src/main/webapp、public或者static的路径
其中优先级为src/main/webapp>public>static
需要修改到模块目录下
参考:
转载地址:http://jiwof.baihongyu.com/