首先,贴上错误前端错误代码:
<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); }贴上代码,我是这样解决的,希望大家可以评论讨论,小弟洗耳恭听!