1. 程式人生 > 實用技巧 >springboot的Not allowed to load local resource問題

springboot的Not allowed to load local resource問題

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