溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Java上傳文件錯誤java.lang.NoSuchMethodException怎么辦

發(fā)布時間:2021-07-12 14:50:50 來源:億速云 閱讀:490 作者:小新 欄目:編程語言

小編給大家分享一下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)

解決辦法:在方法里加上參數(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;
  }

看完了這篇文章,相信你對“Java上傳文件錯誤java.lang.NoSuchMethodException怎么辦”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI