首先,貼上錯誤前端錯誤碼:
<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); }貼上代碼,我是這樣解決的,希望大家可以評論討論,小弟洗耳恭聽。