ngfileupload 上傳檔案對家java restful 介面的:required ‘file’ not present 問題解決

來源:互聯網
上載者:User

首先,貼上錯誤前端錯誤碼:

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.