Java上传文件错误java.lang.NoSuchMethodException的解决办法
错误详情:
java.lang.NoSuchMethodException:[Lorg.springframework.web.multipart.MultipartFile;.() atjava.lang.Class.getConstructor0(UnknownSource) atjava.lang.Class.getDeclaredConstructor(UnknownSource) atorg.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:104) atorg.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:137) atorg.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:80)
解决办法:在方法里加上参数注解@RequestParam
这个错误是在使用wangEditor配置多文件上传的时候出现的,使用单个文件上传没有这个问题。
直接使用多文件上传一直报错,就用了单文件循环。
代码如下:
@RequestMapping(value="uploadFilesForWEditor",method={RequestMethod.GET,RequestMethod.POST})
@ResponseBody
publicstaticMapuploadFilesForWEditor(@RequestParam("files")MultipartFile[]files,HttpServletRequestrequest,HttpServletResponseresponse){
Mapmap=newHashMap<>();
Listurl=newArrayList<>();
for(inti=0;i0){
map.put("errno",0);
map.put("msg","上传成功");
map.put("data",url);
}else{
map.put("errno",1);
map.put("msg","上传失败");
map.put("data",url);
}
returnmap;
}
FileUploadUtils:
publicstaticStringfileUpload(MultipartFilefile,HttpServletRequestrequest,HttpServletResponseresponse){
//获取图片的原名字
StringoldName=file.getOriginalFilename();
StringtimeName=System.currentTimeMillis()+"_";
StringnewName=timeName+oldName;
//获取项目的路径在项目路径下新建文件夹
Stringpath="D:/uploadFile";
//新建uploadFile文件夹
FileparentPath=newFile(path);
if(!parentPath.exists()){
parentPath.mkdirs();
}
Stringsrc="";
try{
file.transferTo(newFile(parentPath,newName));
FiletheFile=newFile(parentPath+"/"+newName);
if(theFile.exists()){
//拼接图片的相对路径作为URL
src="/"+newName;
}else{
src="";
}
}catch(IllegalStateExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}
returnsrc;
}
记录错误。
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。