Java上传文件错误java.lang.NoSuchMethodException怎么办

发布时间:2021-07-12 14:50:50 作者:小新
来源:亿速云 阅读:480

小编给大家分享一下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;
  }

看完了这篇文章,相信你对“Java上传文件错误java.lang.NoSuchMethodException怎么办”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. Caused by: java.lang.NoSuchMethodException: onClick_Flashlight [class android.view.View]
  2. webuploader上传文件错误提示

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:centos8中怎么修改网卡配置

下一篇:centos 6.3中怎么搭建一个vsftpd服务器

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》