关于Not allowed to load local resource问题解决方案
当页面上直接访问项目外的资源时会出现这个问题,它不允许我们直接访问系统中的资源,找了一些资料后配置了一个虚路径解决这个问题。
package com.example.wxhy.controller;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @Authof: Huangchenyang
* @Date: Create in 16:32 2018/8/8
*/
@Configuration
public class MyWebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/imagem/**").addResourceLocations("file:D:/code/barcode/");
registry.addResourceHandler("/image/**").addResourceLocations("file:D:/image/");
}
}
文件保存在实目录file:D:/code/barcode/下,访问的时候使用虚路径/image,比如文件名为1.png,就直接/image/1.png就ok了。
当然也可以写成配置文件的形式,但是还是要在类里面读取,索性直接写成配置类了
容器为tomcat的时候打开tomcat服务界面 在下面选择Modules 然后选择Add Web Module选择Browse 选择你的资源目录,然后填写path也是应该可以的,但是我没有测试