Spring资源路径问题

81 阅读1分钟

获取:

String basePath = ResourceUtils.getURL("classpath:").getPath() + "static/upload/" );

该代码获取的是target/classes/staitc/upload 所以说ResourceUtils.getURL("classpath:")其实就是去获取target/classes 至于resources/staitc/upload下只能通过绝对路径或者文件路径去获取

访问:

当文件上传到target/classes/staitc/upload目录下该怎么获取呢 需要配置

spring:
    web:
      resources:
        static-locations: classpath:/static/upload

然后通过http://192.168.56.1:8888/a.webp直接访问。 如果配置成static-locations: classpath:/static 那么就需要这样访问

http://192.168.56.1:8888/upload/a.webp

一般文件上传都是上传到图片服务器或者本地文件,不上传到classpath。 然后返回绝对路径。

Snipaste_2023-04-19_10-59-27.png

Snipaste_2023-04-19_10-59-38.png