解决SpringBoot图片上传无法立即显示,需要重启才能访问

新建类ImgesUploadConfig 继承WebMvcConfigurer

代码如下,复制粘贴稍作改动即可:

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 ImagesUploadConfig implements WebMvcConfigurer {
    /**
     * 解决图片上传无法立即显示,而需要重启才能访问的问题。
     * 这是因为对服务器的保护措施导致的,服务器不能对外部暴露真实的资源路径,需要配置虚拟路径映射访问。
     */

    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //获取项目路径  如F:\dmx\AutoBurdManage\autoburn
        String property = System.getProperty("user.dir");
        System.out.println("---------------------->"+property);
        //获取文件的真实路径  printImg为我项目中图片存储的路径
        String path = property+"\\src\\main\\resources\\static\\printImg\\";
        //       /images/**是对应resource下工程目录
        registry.addResourceHandler("/printImg/**").addResourceLocations("file:"+path);
    }
}
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇