通过请求路径访问项目外的本地文件

通过请求路径访问项目外的本地文件

package com.ydy.file.config;

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 FileInterceptorConfig implements WebMvcConfigurer {

    /**
     * 上传的图片在F盘下的file目录下,访问路径如:http://localhost:8080/file/d3cf0281-bb7f-40e0-ab77-406db95ccf2c.jpg
     *  其中file表示访问的前缀。"file:F:/file/"是文件真实的存储路径
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //图片
        registry.addResourceHandler("/image/**").
                addResourceLocations("file:/image/");
        //pdf文件
        registry.addResourceHandler("/pdf/**").
                addResourceLocations("file:/pdf/");
        //视屏
        registry.addResourceHandler("/video/**").
                addResourceLocations("file:/video/");
        //word文件
        registry.addResourceHandler("/word/**").
                addResourceLocations("file:/word/");
        //excel文件
        registry.addResourceHandler("/excel/**").
                addResourceLocations("file:/excel/");
    }
}

效果如下图:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_40917075/article/details/115698480