SpringBoot vue图片上传不能立即回显问题解决
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot vue图片上传不能立即回显问题解决
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
最開始項目是放在eclipse之中的、springboot項目默認(rèn)把靜態(tài)的文件加載到classpath的目錄下的。而此時我們上傳的圖片并沒有傳入啟動了的項目當(dāng)中去、所以明明路徑是對的、卻訪問不了、在項目重新啟動之后項目會打成新的jar包、這個時候上一次上傳的圖片才會正常顯示。
解決方法:配置靜態(tài)資源路徑訪問。配置虛擬路徑。
后端上傳文件代碼:
@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里面配置、然后讀取、因為我這是簡單的畢業(yè)設(shè)計項目、所以就直接寫死了。配置WebMvcConfigurationSupport重寫addResourceHandlers即可實現(xiàn)。
/*** springboot 2.0配置WebMvcConfigurationSupport之后,會導(dǎo)致默認(rèn)配置被覆蓋,要訪問靜態(tài)資源需要重寫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);}?問題解決。簡單記錄一下。
總結(jié)
以上是生活随笔為你收集整理的SpringBoot vue图片上传不能立即回显问题解决的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js中的if与Java中的if_JS直接
- 下一篇: pandas python groupb