1. 程式人生 > >springboot 配置檔案虛擬路徑 供外部訪問

springboot 配置檔案虛擬路徑 供外部訪問

配置虛擬路徑 供外部訪問圖片 視訊等檔案

第一步:配置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/  為檔案絕對路徑 

第二步:新增 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: 對檔案的路徑進行配置, 建立一個虛擬路徑/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