欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

springboot文件虚拟路径映射方式

时间:2022-04-24 10:17:14|栏目:JAVA代码|点击:

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

上一篇:SpringDataJpa:JpaRepository增删改查操作

栏    目:JAVA代码

下一篇:SpringBoot中的yaml语法及静态资源访问问题

本文标题:springboot文件虚拟路径映射方式

本文地址:http://www.codeinn.net/misctech/200077.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有