[英]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.