欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot文件虛擬路徑映射方式

 更新時(shí)間:2021年08月20日 11:16:39   作者:小球?qū)W前端  
這篇文章主要介紹了springboot文件虛擬路徑映射方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

springboot文件虛擬路徑映射

在application.properties配置文件中配置

spring.http.multipart.location= D:/
spring.mvc.static-path-pattern=/**
spring.resources.static-locations=  classpath:/,  file:${spring.http.multipart.location}

表示:讀取文件時(shí)從我們的磁盤(pán)D中讀取文件,例如:我們數(shù)據(jù)庫(kù)中存取的文件路徑為:

/2018/06/21//danPicture/cefb656f8cf542968107ca51e15c4ee5-1529540867019.png

那么在頁(yè)面中展示的圖片的路徑實(shí)際上是我們本機(jī)電腦的路徑:

D:/2018/06/21//danPicture/cefb656f8cf542968107ca51e15c4ee5-1529540867019.png

就是說(shuō)會(huì)自動(dòng)幫我們做一個(gè)拼接的操作?。。?!

springboot 配置文件虛擬路徑 供外部訪問(wèn)

配置虛擬路徑 供外部訪問(wèn)圖片 視頻等文件

第一步:配置application.yml

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/ 為文件絕對(duì)路徑

第二步:添加 Configuration 文件

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: 對(duì)文件的路徑進(jìn)行配置, 創(chuàng)建一個(gè)虛擬路徑/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 沒(méi)有配置

server:
  servlet:
    context-path: /sell

瀏覽器訪問(wèn) http://localhost:8080/Path/11.html

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論