问题描述

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()为临时目录问题解决方案相关推荐

  1. Spring Boot 内置Tomcat——集成PHP解决方案

    Demo:https://gitee.com/shentuzhigang/mini-project/tree/master/springboot-embed-tomcat-php-demo 问题分析 ...

  2. Spring Boot——内置Tomcat配置阿里云免费SSL证书(PFX格式证书)[启用HTTPS协议]

    基本概念 SSL证书:SSL证书是数字证书的一种,类似于驾驶证.护照和营业执照的电子副本.因为配置在服务器上,也称为SSL服务器证书. SSL 证书就是遵守 SSL协议,由受信任的数字证书颁发机构CA ...

  3. Spring Boot内置Tomcat设置超时时间

    最近有个小工程扫描出一个安全漏洞, SlowHttp慢速攻击的,需要修改 Tomcat 的配置,也正好关于 Tomcat 的参数调优,正好记录一下. 漏洞信息 查了一下这个漏洞,漏洞有两个解决方法, ...

  4. Spring Boot 内置Tomcat——IntelliJ IDEA中配置模块目录设为文档根目录(DocumentRoot)解决方案

    源码分析 org.springframework.boot.web.servlet.server.DocumentRoot /*** Returns the absolute document roo ...

  5. 项目部署—移除Spring Boot内置Tomcat,部署到云服务器Tomcat

        以往部署Java web项目到阿里云服务器时,直接将项目打包成war包,放到阿里云服务器中tomcat的webapps目录下,就可以访问了.   SpringBoot默认给我们提供了内置tom ...

  6. Spring Boot 内置Tomcat——集成JSP解决方案

    解决方案 一.创建webapp目录 在src/main下创建webapp目录,用于存放jsp文件.这就是一个普通的目录,无需执行Mark Directory As 二.创建JSP 1.指定web资源目 ...

  7. Spring Boot内置Tomcat的静态资源配置(在页面中显示项目外的某个图片)

    哇~我现在只想长长的舒一口气,终于解决了 ~    记录一下 好,下面开始我的第一个博客,写的不好还请大家见谅~ Spring Boot项目是在官网自动生成的,目录如下: 在红线圈住的"ap ...

  8. Spring Boot内嵌tomcat关于getServletContext().getRealPath获取得到临时路径的问题

    问题记录 问题场景:修改头像业务,文件上传 代码 // 上传的文件.../upload/文件.png//在javaWeb中我们会用HttpServletRequest来获取上下文路径,当然sessio ...

  9. 修改Spring boot内置的tomcat端口

    介绍两种种简单的修改spring boot内置端口的方法: 第一种:在入口直接设置端口,代码如下所示: public static void main(String[] args){SpringApp ...

最新文章

  1. 拦截锚点修改url_前端系列课程(2)-网络基础概念(URL)
  2. Android---手动创建线程与GUI线程同步(三)
  3. ES6公用分页组件的封装及应用举例
  4. zabbix java api
  5. vue项目微信分享之后路由链接被破坏怎么办
  6. windows安装grafana
  7. init 0 init1 init 3 init 5 init 6 这几个启动级别都代表什么意思
  8. CSDN博客排名不更新,谈谈重构的做法
  9. Python 支付宝转账到银行卡二维码制作步骤分享
  10. matlab画圆的命令_matlab画圆命令
  11. JavaScript ES6介绍
  12. 微软出品的Python小白神器,真香!
  13. android动态权限依赖库,动态申请app权限:郭霖大神的PermissionX库带你告别原生
  14. Huffman Tree
  15. SIM不识卡简单分析流程
  16. P型半导体、N型半导体定义
  17. C++ decorator(装饰)模式
  18. 什么是Subject
  19. DataFrame.drop_duplicates操作中的inplace参数
  20. 腾讯全资子公司 CODING 2021 届春季校招补录全面启动!

热门文章

  1. 广告影响网站打开速度解决方案
  2. Python爬取网站用户手机号_利用python爬取慕课网站上面课程
  3. java chars_Java getChars() 方法 - Java 基础教程
  4. Android Studio同步失败设置代理,Android Studio如何设置代理?
  5. java 打印对象属性 工具类_关于java实现任意对象输出字符串的工具类ObjectUtils用户打印日志、接口调试及监控等...
  6. Linux脚本让我选择文件,linux – 用于选择文件和打印文件大小的Awk脚本
  7. 原字体_在包装上玩转字体
  8. java实现多对多关系的方法_java – 如何在JPA中实现复杂的多对多关系?
  9. java解析字符串_用Java解析字符串有哪些不同的方法?
  10. Docker安装mysql并连接