springboot图片的虚拟路劲设置为相对路劲:实现前端后分离都可以使用图片

在src/main/java/config

核心点:classpath:表示从src/main/resources/

package com.macro.mall.config;//package com.macro.mall.config;

        import com.macro.mall.common.BasicData;
        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 WebMvcConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //其中image表示访问的前缀。"file:F:/img/"是文件真实的存储路径
        registry.addResourceHandler("/file/**").addResourceLocations("classpath:/upload_file/");
    }
}

图片存储在/resources/upload_file/

 同理log4j和文件上传也可以是相对路径

#(3)日志配置
logging:
  level:
    com:
      #控制不同包下的日志级别(日志级别由低到高 trace < debug < info < warn < error)
      itheima: error
      #可以指定完整的路径(logging.path和logging.file 不能同时使用,同时出现,则logging.file生效)
  file:
    #我电脑本地的
    name: classpath:\lg4j\\springboot.log
#(4)文件上传
files:
  upload:
    chunkSize: 5 242 880
    #我电脑本地的
    path: classpath:\upload_file\

猜你喜欢

转载自blog.csdn.net/weixin_54691198/article/details/127887403