springboot中配置addResourceHandler和addResourceLocations,使得可以從磁盤中讀取圖片、視頻、音頻等

磁盤目錄






WebMvcConfig的代碼

//對靜態資源的配置
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {

   String os = System.getProperty("os.name");

   if (os.toLowerCase().startsWith("win")) {  //如果是Windows系統
      registry.addResourceHandler("/smallapple/**")
            // /apple/**表示在磁盤apple目錄下的所有資源會被解析爲以下的路徑
            .addResourceLocations("file:G:/itemsource/smallapple/") //媒體資源
            .addResourceLocations("classpath:/META-INF/resources/");  //swagger2頁面
   } else {  //linux 和mac
      registry.addResourceHandler("/smallapple/**")
            .addResourceLocations("file:/resources/smallapple/")   //媒體資源
            .addResourceLocations("classpath:/META-INF/resources/");  //swagger2頁面;
   }
}


數據庫中路徑




測試:






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章