javascript
Spring Boot 内置Tomcat——getServletContext().getRealPath()为临时目录问题解决方案
問題描述
?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 archivefile = (file != null) ? file : getWarFileDocumentRoot();// If not a war archive maybe it is an exploded warfile = (file != null) ? file : getExplodedWarFileDocumentRoot();// Or maybe there is a document root in a well-known locationfile = (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
需要修改到模塊目錄下
參考:Spring Boot 內置Tomcat——IntelliJ IDEA中配置模塊目錄為文檔根目錄(DocumentRoot)解決方案
參考文章
Spring Boot 內置Tomcat——IntelliJ IDEA中配置模塊目錄為文檔根目錄(DocumentRoot)解決方案
springboot內置Tomcat的getServletContext().getRealPath問題
總結
以上是生活随笔為你收集整理的Spring Boot 内置Tomcat——getServletContext().getRealPath()为临时目录问题解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Boot 内置Tomcat
- 下一篇: Spring Boot 内置Tomcat