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)
解決辦法:在方法里加上參數(shù)注解 @RequestParam
這個錯誤是在使用wangEditor配置多文件上傳的時候出現(xiàn)的,使用單個文件上傳沒有這個問題。
直接使用多文件上傳一直報錯,就用了單文件循環(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; }
記錄錯誤。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接
相關(guān)文章
java使用http實現(xiàn)文件下載學(xué)習(xí)示例
這篇文章主要介紹了java使用http實現(xiàn)文件下載學(xué)習(xí)示例,需要的朋友可以參考下2014-04-04用SpringBoot+Vue+uniapp小程序?qū)崿F(xiàn)在線房屋裝修管理系統(tǒng)
這篇文章主要介紹了用SpringBoot+Vue+uniapp實現(xiàn)在線房屋裝修管理系統(tǒng),針對裝修樣板信息管理混亂,出錯率高,信息安全性差,勞動強度大,費時費力等問題開發(fā)了這套系統(tǒng),需要的朋友可以參考下2023-03-03Java實現(xiàn)ATM系統(tǒng)超全面步驟解讀建議收藏
這篇文章主要為大家詳細介紹了用Java實現(xiàn)簡單ATM機功能,文中實現(xiàn)流程寫的非常清晰全面,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-03-03java計算自然數(shù)中的水仙花數(shù)的方法分享
這篇文章主要介紹了java計算自然數(shù)中的水仙花數(shù)的方法,需要的朋友可以參考下2014-03-03Spring Boot 自定義數(shù)據(jù)源DruidDataSource代碼
這篇文章主要介紹了Spring Boot 自定義數(shù)據(jù)源DruidDataSource代碼,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-10-10