Springboot+Thymeleaf页面打开本地文件404问题

1、问题描述

(1)、首先,我在做文件上传时,存放到项目的src下面,但是在网站上打开页面加载文件时报404错误,后来发现是文件没有同步到target文件夹里面,我便将文件上传路径写到target文件夹里面,
虽然在idea中跑项目没有问题,但是经过打包部署到服务器上,运行后又报404错误,气的脑壳疼。

2、解决方法

(1)、配置虚拟目录
a、在yml文件中写上

prop:
  upload-folder: D:/file/

spring:
	 resources:
      static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${prop.upload-folde}

b、创建个映射类(拦截)

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 图片绝对地址与虚拟地址映射
 */

@Configuration
public class FileConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //文件磁盘图片路径 映射
        //handler为前台访问的目录,locations为files相对应的本地路径
        registry.addResourceHandler("/upload/**").addResourceLocations("file:/D:/file/upload/");
    }
}

PS:
例子:

<img src="/upload/images/Handsome.jpg">
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值