springboot虚拟路径映射 springboot文件虚拟路径映射方式

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

springboot虚拟路径映射 springboot文件虚拟路径映射方式

小球学前端   2021-08-20 我要评论
想了解springboot文件虚拟路径映射方式的相关内容吗,小球学前端在本文为您仔细讲解springboot虚拟路径映射的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:springboot路径,文件虚拟路径,虚拟路径映射,下面大家一起来学习吧。

springboot文件虚拟路径映射

在application.properties配置文件中配置

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

就是说会自动帮我们做一个拼接的操作!!!!

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

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们