日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > vue >内容正文

vue

SpringBoot vue图片上传不能立即回显问题解决

發布時間:2025/3/12 vue 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot vue图片上传不能立即回显问题解决 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

最開始項目是放在eclipse之中的、springboot項目默認把靜態的文件加載到classpath的目錄下的。而此時我們上傳的圖片并沒有傳入啟動了的項目當中去、所以明明路徑是對的、卻訪問不了、在項目重新啟動之后項目會打成新的jar包、這個時候上一次上傳的圖片才會正常顯示。

解決方法:配置靜態資源路徑訪問。配置虛擬路徑。

后端上傳文件代碼:

@RequestMapping("/upload")public R upload(@RequestParam("file") MultipartFile file,String type) throws Exception {if (file.isEmpty()) {throw new EIException("上傳文件不能為空");}String fileExt = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);File upload = new File("D:/work/");if(!upload.exists()) {upload.mkdirs();}String fileName = new Date().getTime()+"."+fileExt;File dest = new File(upload+"/"+fileName);file.transferTo(dest);if(StringUtils.isNotBlank(type) && type.equals("1")) {ConfigEntity configEntity = configService.selectOne(new EntityWrapper<ConfigEntity>().eq("name", "faceFile"));if(configEntity==null) {configEntity = new ConfigEntity();configEntity.setName("faceFile");configEntity.setValue(fileName);} else {configEntity.setValue(fileName);}configService.insertOrUpdate(configEntity);}return R.ok().put("file", fileName);} File upload = new File("D:/work/");這里路徑建議在yml里面配置、然后讀取、因為我這是簡單的畢業設計項目、所以就直接寫死了。

配置WebMvcConfigurationSupport重寫addResourceHandlers即可實現。

/*** springboot 2.0配置WebMvcConfigurationSupport之后,會導致默認配置被覆蓋,要訪問靜態資源需要重寫addResourceHandlers方法*/@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/**").addResourceLocations("classpath:/resources/").addResourceLocations("classpath:/static/").addResourceLocations("classpath:/upload/").addResourceLocations("classpath:/admin/").addResourceLocations("classpath:/front/").addResourceLocations("classpath:/public/");registry.addResourceHandler("/upload/**").addResourceLocations("file:D:/work/");super.addResourceHandlers(registry);}

?問題解決。簡單記錄一下。

總結

以上是生活随笔為你收集整理的SpringBoot vue图片上传不能立即回显问题解决的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。