Java上傳文件錯誤java.lang.NoSuchMethodException的解決辦法
更新時間:2019年01月05日 11:15:45 作者:丶Melody
今天小編就為大家分享一篇關于Java上傳文件錯誤java.lang.NoSuchMethodException的解決辦法,小編覺得內容挺不錯的,現在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
錯誤詳情:
java.lang.NoSuchMethodException: [Lorg.springframework.web.multipart.MultipartFile;.<init>() at java.lang.Class.getConstructor0(Unknown Source) at java.lang.Class.getDeclaredConstructor(Unknown Source) at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104) at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:137) at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:80)
解決辦法:在方法里加上參數注解 @RequestParam
這個錯誤是在使用wangEditor配置多文件上傳的時候出現的,使用單個文件上傳沒有這個問題。
直接使用多文件上傳一直報錯,就用了單文件循環(huán)。
代碼如下:
@RequestMapping(value="uploadFilesForWEditor",method={RequestMethod.GET,RequestMethod.POST}) @ResponseBody public static Map<String,Object> uploadFilesForWEditor(@RequestParam("files")MultipartFile[] files,HttpServletRequest request,HttpServletResponse response){ Map<String,Object> map=new HashMap<>(); List<String> url = new ArrayList<>(); for (int i = 0; i < files.length; i++) { String result=FileUploadUtils.fileUpload(files[i], request, response); if(result!=""){ url.add(result); } } if(url.size()>0){ map.put("errno",0); map.put("msg","上傳成功"); map.put("data",url); }else{ map.put("errno",1); map.put("msg","上傳失敗"); map.put("data",url); } return map; }
FileUploadUtils:
public static String fileUpload(MultipartFile file,HttpServletRequest request,HttpServletResponse response){ //獲取圖片的原名字 String oldName=file.getOriginalFilename(); String timeName=System.currentTimeMillis()+"_"; String newName=timeName+oldName; //獲取項目的路徑 在項目路徑下新建文件夾 String path= "D:/uploadFile"; //新建 uploadFile 文件夾 File parentPath=new File(path); if(!parentPath.exists()){ parentPath.mkdirs(); } String src=""; try { file.transferTo(new File(parentPath,newName)); File theFile=new File(parentPath+"/"+newName); if(theFile.exists()){ //拼接圖片的相對路徑作為URL src="/"+newName; }else{ src=""; } } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return src; }
記錄錯誤。
總結
以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,謝謝大家對腳本之家的支持。如果你想了解更多相關內容請查看下面相關鏈接
相關文章
用SpringBoot+Vue+uniapp小程序實現在線房屋裝修管理系統(tǒng)
這篇文章主要介紹了用SpringBoot+Vue+uniapp實現在線房屋裝修管理系統(tǒng),針對裝修樣板信息管理混亂,出錯率高,信息安全性差,勞動強度大,費時費力等問題開發(fā)了這套系統(tǒng),需要的朋友可以參考下2023-03-03Spring Boot 自定義數據源DruidDataSource代碼
這篇文章主要介紹了Spring Boot 自定義數據源DruidDataSource代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-10-10