當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Springboot加载静态图片
生活随笔
收集整理的這篇文章主要介紹了
Springboot加载静态图片
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?java工具類:
import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;/*** Created by zhangj on 2018/07/31*/ @Configuration public class ShowImage extends WebMvcConfigurerAdapter {/*** 注入圖片存放路徑*/@Value("${upload.path.prefix}")private String prefix;@Value("${upload.path.res}")private String uploadPath;@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {//注:使用此種方法進行路徑配置時,“file:D:/OTA/” 為正確路徑地址 “file:D:/OTA”為錯誤路徑地址,需以反斜杠結尾才可正確加載registry.addResourceHandler("/images/**").addResourceLocations("file:" + prefix + "/");super.addResourceHandlers(registry);} }使用的是注入的方式,所以application中的配置:
upload:path:prefix: d:/app/share/datares: /upload/resource/?
加載靜態資源文件時使用springboot使用的方式,需注意點為拼接的路徑需以反斜杠結尾,不然訪問不到正確的路徑地址,設置完成之后,即可進行訪問。
如圖片路徑地址為:file:///D:/app/share/data/upload/resource/20180919/b104721b07d64501a6d24215dbc70207.bmp
需將?file:///D:/app/share/data 更改為?http://localhost:8765/images?即可成功訪問。
總結
以上是生活随笔為你收集整理的Springboot加载静态图片的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: R语言假设检验完整入门教程
- 下一篇: Spring Security完成安全认