springboot通過url地址訪問專案外部檔案的配置
阿新 • • 發佈:2018-12-19
1 新增配置類
package com.ps.learn.socketio.config; 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.WebMvcConfigurer; /** * Author:ZhuShangJin * Date:2018/12/19 */ @Configuration public class WebMvcConfiguration implements WebMvcConfigurer { @Value("${out.resource.path}") private String path; /** * 訪問外部檔案配置,訪問D盤下檔案 */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //配置server虛擬路徑,handler為jsp中訪問的目錄,locations為image相對應的本地路徑 registry.addResourceHandler("/image/**").addResourceLocations(path); } }
2、application.properties配置檔案新增外部檔案地址
out.resource.path=file:D://upload/
3 訪問
我們將1.png檔案上傳到D盤的upload資料夾後,那麼在頁面端訪問則通過:http://127.0.0.1/image/1.png