欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java上傳文件錯誤java.lang.NoSuchMethodException的解決辦法

 更新時間:2019年01月05日 11:15:45   作者:丶Melody  
今天小編就為大家分享一篇關(guān)于Java上傳文件錯誤java.lang.NoSuchMethodException的解決辦法,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧

錯誤詳情:

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)

解決辦法:在方法里加上參數(shù)注解 @RequestParam

這個錯誤是在使用wangEditor配置多文件上傳的時候出現(xiàn)的,使用單個文件上傳沒有這個問題。

直接使用多文件上傳一直報錯,就用了單文件循環(huán)。

代碼如下:

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

記錄錯誤。

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,謝謝大家對腳本之家的支持。如果你想了解更多相關(guān)內(nèi)容請查看下面相關(guān)鏈接

相關(guān)文章

最新評論