Spring boot文件路徑映射配置代碼實(shí)例
springboot配置本地資源映射路徑需要配置一下映射資源位置,下面來(lái)介紹一下過(guò)程。
1.添加配置類
package org.jcut.tools;import org.springframework.beans.factory.annotation.Value;import org.springframework.stereotype.Component;import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;@Componentpublic class MvcConfing implements WebMvcConfigurer { @Value('${imgpath}') String path; @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { //當(dāng)請(qǐng)求服務(wù)器中帶有/imgs/的時(shí)候就是到指定路徑中找出對(duì)應(yīng)名字對(duì)應(yīng)圖片,其中**就代表匹配多層路徑下的名字registry.addResourceHandler('/imgs/**').addResourceLocations('file:'+path); }}
2.圖片上傳代碼
//添加展示圖片 String t_img=UUID.randomUUID().toString();//商品展示圖片id boolean flag=false; for(MultipartFile ff:file1) { String tImgId=UUID.randomUUID().toString();//寫入圖片名字的時(shí)候最好把獲取圖片映射名稱寫進(jìn)去,方便獲取圖片 timg.insert(new TImg(tImgId, t_img, 'imgs/'+tImgId+'.jpg')); try {ff.transferTo(new File(path+tImgId+'.jpg'));flag=true; } catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace(); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. PHP如何打印跟蹤調(diào)試信息2. 解決AJAX返回狀態(tài)200沒(méi)有調(diào)用success的問(wèn)題3. springboot的yml配置文件通過(guò)db2的方式整合mysql的教程4. Python TestSuite生成測(cè)試報(bào)告過(guò)程解析5. 簡(jiǎn)述JAVA同步、異步、阻塞和非阻塞之間的區(qū)別6. 利用php來(lái)自動(dòng)調(diào)用不同服務(wù)器上的flash7. 深入了解JAVA 軟引用8. 在JSP中使用formatNumber控制要顯示的小數(shù)位數(shù)方法9. Java JTable 實(shí)現(xiàn)日歷的示例10. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法
