本篇內容主要講解“springboot文件的虛擬路徑映射方式”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學習“springboot文件的虛擬路徑映射方式”吧!
springboot文件虛擬路徑映射
在application.properties配置文件中配置
springboot 配置文件虛擬路徑 供外部訪問
第一步:配置application.yml
第二步:添加 Configuration 文件
重啟
spring.http.multipart.location= D:/
spring.mvc.static-path-pattern=/**
spring.resources.static-locations= classpath:/, file:${spring.http.multipart.location}表示:讀取文件時從我們的磁盤D中讀取文件,例如:我們數據庫中存取的文件路徑為:
/2018/06/21//danPicture/cefb656f8cf542968107ca51e15c4ee5-1529540867019.png
那么在頁面中展示的圖片的路徑實際上是我們本機電腦的路徑:
D:/2018/06/21//danPicture/cefb656f8cf542968107ca51e15c4ee5-1529540867019.png
就是說會自動幫我們做一個拼接的操作?。。?!
配置虛擬路徑 供外部訪問圖片 視頻等文件
spring: mvc: static-path-pattern: /** resources: static-locations: classpath\:/META-INF/resources/,classpath\:/resources/,classpath\:/static/,classpath\:/public/,file\:F:/wechatProject/upload/
其中 F:/wechatProject/upload/ 為文件絕對路徑
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
@Configuration
public class MyWebAppConfiguration extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
/**
* @Description: 對文件的路徑進行配置, 創建一個虛擬路徑/Path/** ,即只要在<img src="/Path/picName.jpg" />便可以直接引用圖片
*這是圖片的物理路徑 "file:/+本地圖片的地址"
* @Date: Create in 14:08 2017/12/20
*/
registry.addResourceHandler("/Path/**").addResourceLocations("file:/F:/wechatProject/upload/");
super.addResourceHandlers(registry);
}
}如果application.yml 沒有配置
server: servlet: context-path: /sell
瀏覽器訪問 http://localhost:8080/Path/11.html
到此,相信大家對“springboot文件的虛擬路徑映射方式”有了更深的了解,不妨來實際操作一番吧!這里是億速云網站,更多相關內容可以進入相關頻道進行查詢,關注我們,繼續學習!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。