ngfileupload 上传文件对家java restful 接口的:required ‘file’ not present 问题解决

首先,贴上错误前端错误代码:

	<button class="btn btn-primary">
        	<i class="fa fa-upload m-r-sm"></i> 上传文件
        	<div style="position: absolute; top: 0px; left: 0px; width: 150px; height: 57px; overflow: hidden; bottom: auto; right: auto;">
       		   <input data-ng-disabled="false"  class="form-control" type="file" name="file"
                	 ngf-select="uploadFiles($file)"
                 	ng-model="uploadfile"  ngf-max-size="1.1GB" placeholder=""
                 	style="height:47px;position: absolute;opacity:0;cursor:pointer" required>
       		 </div>
      </button><button class="btn btn-primary">
        	<i class="fa fa-upload m-r-sm"></i> 上传文件
        	<div style="position: absolute; top: 0px; left: 0px; width: 150px; height: 57px; overflow: hidden; bottom: auto; right: auto;">
       		   <input data-ng-disabled="false"  class="form-control" type="file" name="file"
                	 ngf-select="uploadFiles($file)"
                 	ng-model="uploadfile"  ngf-max-size="1.1GB" placeholder=""
                 	style="height:47px;position: absolute;opacity:0;cursor:pointer" required>
       		 </div>
      </button>

后端restful 接口部分代码:

 @RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public UploadSuccess uploadExcel(@RequestParam("file") MultipartFile files) {
			..........
}

调了半天始终出现错误:

始终说file 参数不存在,该问题主要由html 代码里面的 name=“file”引起,只需要把那么改为不叫file 的其它值就可以了。

上述问题是解决了,但是如果我需要上传多文件把MultipartFile改为数组接不住ng-fileupload上传上来的文件流,最后解决是注入HttpServeletRquest 直接通过这个获取到part的文件流才获取到文件。



    @RequestMapping(value = "/upload", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public UploadSuccess uploadExcel(HttpServletRequest request) {

        List<Part> listPart = new ArrayList<>();
        try {
            Iterator its = request.getParts().iterator();
            while (its.hasNext()) {
                Part part = (Part) its.next();
                listPart.add(part);
            }
            logger.info("file number ====>" + listPart.size());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ServletException e) {
            e.printStackTrace();
        }
        return uploadService.xlsxExcelDeal(listPart);
    }

贴上代码,我是这样解决的,希望大家可以评论讨论,小弟洗耳恭听!
 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值