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配置多文件上传的时候出现的,使用单个文件上传没有这个问题。
直接使用多文件上传一直报错,就用了单文件循环。
代码如下:
@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+nginx+https+linux实现负载均衡加域名访问简单测试
这篇文章主要介绍了springboot+nginx+https+linux实现负载均衡加域名访问简单测试,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值 ,需要的朋友可以参考下2019-05-05WebSocket+Vue+SpringBoot实现语音通话的使用示例
本文主要介绍了WebSocket+Vue+SpringBoot实现语音通话的使用示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2023-11-11SpringBoot整合redis+Aop防止重复提交的实现
Spring Boot通过AOP可以实现防止表单重复提交,本文主要介绍了SpringBoot整合redis+Aop防止重复提交的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧2023-07-07SpringCloudGateway Nacos GitlabRunner全自动灰度服务搭建发布
这篇文章主要为大家介绍了SpringCloudGateway Nacos GitlabRunner全自动灰度服务搭建和发布实战详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪2023-04-04
最新评论