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)

解决办法:在方法里加上参数注解 @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;
    }

记录错误。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值