以前写SSM项目的时候,项目结构是webapp,所以在代码里./就是相对路径。
但是在resources目录下,这样的写法是

resources目录项目

1. ./表示什么
Springboot项目下载/上传路径整理
image.png

Springboot项目下载/上传路径整理
image.png

实验表明, 在resources目录结构下./表示项目源码根目录

2. request.getServletContext().getContextPath()表示什么
Springboot项目下载/上传路径整理
image.png

Springboot项目下载/上传路径整理
image.png

这里我们可以看出它输出的是空,那么我们用这个路径,下载个东西看下它会在哪里
Springboot项目下载/上传路径整理
image.png

Springboot项目下载/上传路径整理
image.png

它放在了磁盘根目录下了(我的项目在C盘的code目录里)

3. WebUtils.getRealPath(servletContext, path)
Springboot项目下载/上传路径整理
image.png
Springboot项目下载/上传路径整理
image.png

会发现有个'点'目录,所以我们path里面就别要点了
Springboot项目下载/上传路径整理
image.png
Springboot项目下载/上传路径整理
image.png
4. request.getServletContext().getRealPath("");
Springboot项目下载/上传路径整理
image.png
Springboot项目下载/上传路径整理
image.png

Springboot项目下载/上传路径整理
image.png
Springboot项目下载/上传路径整理
image.png
5. 自定义目录

在application.yml里添加

web:
  upload-path: ./admin-server/src/main/resources/static/download/
...
spring:
  resources:
    static-locations: classpath:/resources/,classpath:/static/,file:${web.upload-path}

在项目里

@Value("${web.upload-path}")
    private String path;

就可以做指定目录下载上传了。

相关文章:

  • 2022-12-23
  • 2021-11-02
  • 2021-12-03
  • 2021-04-12
  • 2021-07-26
  • 2021-04-06
  • 2021-12-24
  • 2021-12-22
猜你喜欢
  • 2021-12-17
  • 2021-06-02
  • 2021-06-13
  • 2022-01-07
  • 2022-01-08
  • 2021-04-16
  • 2021-05-03
相关资源
相似解决方案