springboot專案實現靜態檔案對映任意路徑
阿新 • • 發佈:2019-01-23
這樣實現的效果是比如我們圖片是在電腦中d盤下。我想用localhost:8080/files/img.jpg訪問那個圖片。
首先在專案中新增如下java檔案
@Component
class WebConfigurer extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//配置server虛擬路徑,handler為jsp中訪問的目錄,locations為files相對應的本地路徑 registry.addResourceHandler("/files/**").addResourceLocations("本地磁碟路徑如:file:///D:/filesdir/");
}
}
這樣當我們在頁面中使用<img src="${ctx}/files/img.jpg"/>
的時候就能看到圖片了(${ctx}相當於localhost:8080)