@Configuration
public class MyPicConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry){
registry.addResourceHandler("/upload/**").addResourceLocations("file:\\IdeaPro\\tiger-springboot\\d6\\src\\main\\resources\\static\\");
}
}
配置虛擬路徑的類
下面是yml屬性檔案
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/tiger_springboot
username: root
password: root
jpa:
show-sql: true
servlet:
multipart:
enabled: true
max-file-size: 20MB
max-request-size: 20MB
resources:
static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:${upload.image-path}
thymeleaf:
prefix: classpath:/templates
suffix: .html
mode: HTML5
encoding: UTF-8
cache: false
upload:
image:
path: F:/upload/
fileUpload:
rootSavePath: F://upload/
rootHttpPath: http://localhost:8080/upload/
大佬們看看哪里錯了
uj5u.com熱心網友回復:
上傳到代碼下的目錄唯一的問題是不在版本控制之下,練手專案倒是無所謂為啥重啟才能讀到,你是容器啟動還是代碼直接啟動?
uj5u.com熱心網友回復:
我是用啟動類直接啟動的
uj5u.com熱心網友回復:
靜態資源應該要主動觸發重繪才能讀取到的,所以上傳的東西最好不要丟代碼一起,實際上打包都是jar,你那些目錄都在jar中,更加無法上傳
如果是war,非war包里資源會被覆寫
uj5u.com熱心網友回復:
上傳到resources下面的路徑需要重新編譯才能看到圖片,所以要重啟服務器解決辦法:在上傳圖片時直接通過String realPath = ClassUtils.getDefaultClassLoader().getResource("static").getPath()+"/images";該方法直接獲取圖片的編譯路徑,把圖片上傳到編譯好的檔案夾下即可解決
uj5u.com熱心網友回復:
#5
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/101850.html
標籤:Java相關
上一篇:大資料的學習路線是怎么樣的?
