SpringBoot訪問外部文件及默認(rèn)路由問題
SpringBoot訪問外部文件及默認(rèn)路由
1 新增配置類
package com.pibigstar.common.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.pibigstar.common.Constant;
@Configuration
public class WebConfig implements WebMvcConfigurer{
/**
* 訪問外部文件配置,訪問D盤下文件
*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
//配置server虛擬路徑,handler為jsp中訪問的目錄,locations為files相對(duì)應(yīng)的本地路徑
registry.addResourceHandler("/files/**").addResourceLocations("file:///D:upload/");
}
/**
* 配置默認(rèn)路由
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
//將瀏覽器的默認(rèn)行為重定向到主頁
registry.addViewController("/").setViewName("redirect:/index.htm");
//測試頁面
registry.addViewController("/test.htm").setViewName("/test.jsp");
}
}2 訪問
我們將test.jpg文件上傳到D盤的upload文件夾后,那么在頁面端訪問則通過:localhost:8080/files/test.jpg
springboot訪問項(xiàng)目外部文件配置及失效問題
springboot映射項(xiàng)目外部資源
配置文件:
cbs: ? ? filePath: file:///
配置類:
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.WebMvcConfigurationSupport;
/**
* @description:配置訪問外部文件
* @author: Administrator
* @date: 2019-07-10 16:17
*/
@Configuration
public class WebConfig extends WebMvcConfigurationSupport {
@Value("${cbs.filePath}")
private String filePath;//文件地址
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
System.out.println("文件路徑=="+filePath);
registry.addResourceHandler("/appFile/**").addResourceLocations(filePath);
super.addResourceHandlers(registry);
}
}地址:http://localhost:8080/appFile/D:/tmp/app/1.txt

訪問的時(shí)候把 http://localhost:8080/appFile/ 替換成 file:///
也就是file:///D:/tmp/app/1.txt
下面是訪問結(jié)果(請忽略掉亂碼問題)

但是不知道為什么配置類繼承WebMvcConfigurerAdapter和實(shí)現(xiàn)WebMvcConfigurer 接口都沒有用,繼承 WebMvcConfigurationSupport類才生效
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
spring boot優(yōu)雅集成redisson詳解
這篇文章主要為大家介紹了spring boot優(yōu)雅集成redisson詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-11-11
java中實(shí)現(xiàn)控制臺(tái)打印sql語句方式
這篇文章主要介紹了java中實(shí)現(xiàn)控制臺(tái)打印sql語句方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06
Spring容器刷新obtainFreshBeanFactory示例詳解
這篇文章主要為大家介紹了Spring容器刷新obtainFreshBeanFactory示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
SpringBoot整合mybatis通用Mapper+自定義通用Mapper方法解析
這篇文章主要介紹了SpringBoot整合mybatis通用Mapper+自定義通用Mapper方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
IntelliJ IDEA中SpringBoot項(xiàng)目通過devtools實(shí)現(xiàn)熱部署的方法
這篇文章主要介紹了IntelliJ IDEA中SpringBoot項(xiàng)目通過devtools實(shí)現(xiàn)熱部署的方法,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-08-08

