天天看點

List<FileItem> fileItems=sfu.parseRequest(request); fileitems 為,空

最近在使用wangEditor做上傳檔案的功能

List<FileItem> fileItems=sfu.parseRequest(request); fileitems 為,空

但是有一點非常的讓人難受,我在上傳檔案的時候每個方面代碼都是沒有問題的

比如前台:

var editor = new wangEditor('#txtDiv');
  editor.customConfig.uploadImgServer = serviceUrl+'/Shopping/filecontroller/uploadfile';
  editor.customConfig.uploadImgFileName = 'myFileName';
  editor.customConfig.showLinkImg = false;
  editor.create();      

這樣我上傳定義了上傳檔案的接口路徑,背景我使用的是SpringMVC這個架構,接口我是這樣寫的:

@RequestMapping(value = "/uploadfile")
  public void uploadFile(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    log.info("-------------------開始調用上傳檔案uploadfile接口-------------------");
    String path = this.getClass().getClassLoader().getResource("/").getPath();
    int index = path.indexOf("Shopping");
    path = path.substring(0, index + "Shopping".length()) + "/WebContent/resources/upload/";
        DiskFileItemFactory factory = new DiskFileItemFactory();
        ServletFileUpload sfu = new ServletFileUpload(factory);
        sfu.setHeaderEncoding("UTF-8"); // 進行中文問題
        sfu.setSizeMax(1024 * 1024); // 限制檔案大小
        String fileName="";
        try {
           List<FileItem> fileItems=sfu.parseRequest(request);
           for(FileItem item:fileItems){
             fileName=UUID.randomUUID().toString()+item.getFieldName().substring(item.getFieldName().lastIndexOf('.'), item.getFieldName().length());
             item.write(new File(path+"//"+fileName));
           }
        } catch (Exception e) {
            e.printStackTrace();
        }
        //擷取圖檔url位址
        String imgUrl = "resources/upload/"+fileName;
        response.setContentType("text/text;charset=utf-8");
        PrintWriter out = response.getWriter();
        out.print(imgUrl);
        out.flush();
        out.close();
      log.info("-------------------結束調用上傳檔案uploadfile接口-------------------");
  }      

看樣子每個方面都很好,但是有點問題是在上傳檔案的時候背景擷取不到檔案,我打斷點看了看發現的是:

List&lt;FileItem&gt; fileItems=sfu.parseRequest(request); fileitems 為,空

繼續閱讀