在做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文件,就可以正常使用了!