SpringBoot访问外部文件及默认路由问题

作者:跟派大星学编程 时间:2021-08-12 10:58:01 

SpringBoot访问外部文件及默认路由

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相对应的本地路径    
registry.addResourceHandler("/files/**").addResourceLocations("file:///D:upload/");  
}
/**
*配置默认路由
*/
@Override
   public void addViewControllers(ViewControllerRegistry registry) {
       //将浏览器的默认行为重定向到主页
       registry.addViewController("/").setViewName("redirect:/index.htm");
       //测试页面
       registry.addViewController("/test.htm").setViewName("/test.jsp");
}
}

2 访问

我们将test.jpg文件上传到D盘的upload文件夹后,那么在页面端访问则通过:localhost:8080/files/test.jpg

springboot访问项目外部文件配置及失效问题

springboot映射项目外部资源

配置文件:

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

SpringBoot访问外部文件及默认路由问题

访问的时候把 http://localhost:8080/appFile/ 替换成 file:///

也就是file:///D:/tmp/app/1.txt

下面是访问结果(请忽略掉乱码问题)

SpringBoot访问外部文件及默认路由问题

但是不知道为什么配置类继承WebMvcConfigurerAdapter和实现WebMvcConfigurer 接口都没有用,继承 WebMvcConfigurationSupport类才生效

来源:https://blog.csdn.net/junmoxi/article/details/80812457

标签:SpringBoot,访问,外部文件,默认路由
0
投稿

猜你喜欢

  • Springboot深入讲解nocos的整合与使用

    2022-06-05 05:11:27
  • Java List的remove()方法踩坑

    2021-05-27 05:17:58
  • Android进阶Handler应用线上卡顿监控详解

    2022-12-21 11:31:00
  • java日期工具类实例分享

    2023-05-17 17:50:40
  • Java中泛型的示例详解

    2023-10-27 02:54:24
  • ijkPlayer播放器的全自动编译脚本及最终编译包

    2023-01-10 13:52:18
  • Android 自定义布局竖向的ViewPager的实现

    2022-12-30 19:56:17
  • android自由改变Dialog窗口位置的方法

    2021-09-13 04:19:04
  • java实现人员信息管理系统

    2023-11-02 05:21:31
  • java各种类型对象占用内存情况分析

    2023-08-22 10:32:05
  • C#实现语音视频录制-附demo源码

    2023-10-04 13:22:03
  • Gson之toJson和fromJson方法的具体使用

    2021-07-20 16:28:47
  • 浅谈Android性能优化之内存优化

    2023-08-19 04:36:43
  • SpringBoot 配置文件总结

    2021-09-06 13:12:57
  • Java并发编程面试之线程池

    2023-11-11 10:58:33
  • mybatis like模糊查询特殊字符报错转义处理方式

    2023-09-02 21:14:54
  • C语言运算符及其优先级汇总表口诀

    2021-12-12 21:14:27
  • 一文总结Java获取文件后缀名的所有方法

    2022-10-18 16:06:40
  • Spring @Async无法实现异步的解决方案

    2021-10-22 13:32:46
  • java 并发线程个数的如何确定

    2022-01-01 21:52:13
  • asp之家 软件编程 m.aspxhome.com