日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

Spring Boot 内置Tomcat——getServletContext().getRealPath()为临时目录问题解决方案

發布時間:2024/10/5 javascript 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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()为临时目录问题解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。