日韩性视频-久久久蜜桃-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()为临时目录问题解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。

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

主站蜘蛛池模板: 欧美18免费视频 | 国产二页| 乖疼润滑双性初h | 爱爱视频一区二区 | 亚洲精品在线网站 | 一区在线免费 | 国产又粗又猛又爽又黄的视频一 | 欧美人人爽 | 对白刺激国产子与伦 | 99国产成人精品 | 成在线人免费视频 | 公车乳尖揉捏酥软呻吟 | 成人黄色免费视频 | 一区二区三区成人 | 国产精品熟妇一区二区三区四区 | 午夜成年人 | 歪歪6080 | 亚洲欧美高清视频 | 神马午夜影院 | 欧美性三级| 久插网 | 中文字幕不卡av | 国产精彩视频在线观看 | 亚洲国产高清在线 | 羞羞动漫免费观看 | www,久久久| 香蕉视频在线观看www | 五月激情六月丁香 | 日韩福利一区二区 | 国产福利视频在线 | 二区久久| 国产亚洲视频一区 | 成人视屏在线 | 在线观看成人免费 | 日本美女一级视频 | 一级黄色视| 美丽的姑娘在线观看免费 | 日韩精品在线一区 | 樱桃成人精品视频在线播放 | 乱人伦av | 国产黑丝在线 | 日韩美一级片 | 黑人大群体交免费视频 | 国产精品色呦呦 | 伊人影院久久 | 日韩午夜激情电影 | 色呦呦网站在线观看 | 成年人激情视频 | 黄色成人小视频 | 亚洲老女人 | 中文字幕自拍 | 亚洲一级片在线播放 | 黄色激情视频在线观看 | 999xxxx| 精品1区2区 | 久久久久久一区 | 91精品国产综合久久久密臀九色 | 国产在线色| 国产伦精品一区二区三区高清版禁 | 伊人亚洲影院 | 毛利兰被扒开腿做同人漫画 | 天天曰天天操 | 浴室里强摁做开腿呻吟男男 | 午夜偷拍福利视频 | 在线综合av | 国产一区二区三区免费观看视频 | 久久久无码人妻精品一区 | 国产av无码国产av毛片 | av一级黄色 | 99久久久久久久久久 | 美女免费黄色 | 最近最新中文字幕 | 精品国产97 | 激情五月激情 | 青青草日本 | 99ri国产在线 | 久久久欧美精品sm网站 | 爱综合网 | 美女色诱男人激情视频 | 青青草97国产精品麻豆 | 日本精品视频 | 亚洲女女做受ⅹxx高潮 | 在线色站| 2021av| 男男肉耽高h彩漫 | 插插操操 | 9色91| 一进一出视频 | 91禁外国网站 | 久久看av | 亚洲精品视频中文字幕 | 免费簧片在线观看 | 日韩一级伦理片 | 懂色av一区二区三区四区 | 粉嫩av一区二区三区免费观看 | 白丝av| 在线aaa| 日韩免费av一区 | 看日本黄色录像 |