在做springboot图片上传时,发现上传后,浏览器不显示上传图片,后台显示Not allowed to load local resource,然后上网查了下资料,原来是谷歌浏览器安全性比较高,不允许我们直接获得硬盘上的数据,因此我们需要建立虚拟路径来映射文件所在的真实路径。我用的是springboot,关于springboot网上共有俩种解决方案,第一种是添加配置类,如下
package com.tmall.utils;
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 WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/image/**").addResourceLocations("file:F:/桌面/demo/tmall/src/main/resources/templates/page/product/tries_img/");
}
}
然后在访问真是路径是添加"/image/"+文件名
第二种是在进行application.properties的配置
prop.upload-folder=D:/img
spring.resources.static-locations=classpath:/META-INF/resources/,file:${prop.upload-folder}
然后清理下之前的class文件,就可以正常使用了!