当前位置: 移动技术网 > IT编程>开发语言>Java > Java上传文件错误java.lang.NoSuchMethodException的解决办法

Java上传文件错误java.lang.NoSuchMethodException的解决办法

2019年07月19日  | 移动技术网IT编程  | 我要评论

错误详情:

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;
  }

记录错误。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对移动技术网的支持。如果你想了解更多相关内容请查看下面相关链接

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网