天天看点

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 为,空

继续阅读