天天看點

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:)
    at org.springframework.web.method.annotation.ModelAttributeMethodProcessor.createAttribute(ModelAttributeMethodProcessor.java:)
    at org.springframework.web.servlet.mvc.method.annotation.ServletModelAttributeMethodProcessor.createAttribute(ServletModelAttributeMethodProcessor.java:)
           

解決辦法:在方法裡加上參數注解 @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 = ; i < files.length; i++) {
            String result=FileUploadUtils.fileUpload(files[i], request, response);
            if(result!=""){
                url.add(result);
            }
        }
        if(url.size()>){
            map.put("errno",);
            map.put("msg","上傳成功");
            map.put("data",url);
        }else{
            map.put("errno",);
            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;
    }
           

記錄錯誤。