好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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

错误详情:

?

1

2

3

4

5

6

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配置多文件上传的时候出现的,使用单个文件上传没有这个问题。

直接使用多文件上传一直报错,就用了单文件循环。

代码如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

@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:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

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;

   }

记录错误。

总结

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

原文链接:https://blog.csdn.net/qq_36476972/article/details/79524872

查看更多关于Java上传文件错误java.lang.NoSuchMethodException的解决办法的详细内容...

  阅读:33次