[英]How to serve static and uploaded content in Java Spring Boot MVC
我尝试使用以下过程来提供静态内容,但没有运气。
我在 application.properties 中编写了以下代码:
spring.resources.static-locations=classpath:/public/
其中 public 是我的静态和上传内容的目录,如用户个人资料等。
但是当我访问文件时它给了我一个 404 错误,例如:
http://localhost:8082/public/uploads/file/a.png
我也试过:
http://localhost:8082/uploads/file/a.png
但它也不起作用。
如果您在 application.properties 中设置了server.servlet.context-path (或 server.servlet.contextPath,它是相同的),则需要在 URL 中包含这部分,例如:
server.servlet.context-path=/foo
除非你没有设置其他配置参数,否则spring boot应该服务/下的静态资源
因此,如果您的静态资源位于名为public/file
夹下,您会在 url localhost:8082/file/a.png
找到它
如果您有spring.mvc.static-path-pattern
参数集 spring boot 应该在该路径上提供静态资源
在 spring.mvc.static-path-pattern = files(例如)的情况下,它将在http://localhost:8082/files/file/a.png
这是一篇解释所有内容的好文章: https : //www.baeldung.com/spring-mvc-static-resources
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.