欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2021-02-23 15:13:07|栏目:JAVA代码|点击:

错误详情:

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代码

下一篇:Java正则替换手机号代码实例

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

本文地址:http://www.codeinn.net/misctech/68121.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有