servlet中的Apache FileUpload空parseRequest,我将列表设为空(不打印任何数据)

时间:2019-03-05 09:36:57

标签: java servlets postman apache-commons

    protected void doPost(HttpServletRequest request, HttpServletResponse response)

            throws ServletException, IOException {


        String userId = "",firstName = "",lastName= "",mobile= "";
        String fileName= "",filePath= "";

        File storeFileProfileImg = null;
        InputStream uploadedInputStream = null;

        DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setSizeThreshold(MEMORY_THRESHOLD);
        factory.setRepository(new File(System.getProperty("java.io.tmpdir")));

        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setFileSizeMax(MAX_FILE_SIZE);
        upload.setSizeMax(MAX_REQUEST_SIZE);

        String uploadPath = getServletContext().getRealPath("")
                + File.separator + UPLOAD_DIRECTORY;

        File uploadDir = new File(uploadPath);
        if (!uploadDir.exists()) {
            uploadDir.mkdir();
        }

        try {
            @SuppressWarnings("unchecked")
            List<FileItem> formItems = upload.parseRequest(request); //empty list
            if(formItems.isEmpty()) {System.out.println("data is present");}

             else {System.out.println("no data");//printing no data}
    }
}
}

我已经尝试过上面的代码从邮递员读取表单数据,但是每次都将列表设为空。解析请求给了我空值。我如何接收列表中的请求和数据。

在apache文件上传常见问题中,他们写道:

  

为什么parseRequest()不返回任何项目?                                                                              最常见的情况是在请求已被解析或以其他方式处理后。由于输入流早已被该较早的过程消耗,因此Commons FileUpload将不再可用于解析。

但是它不起作用。感谢您的帮助。

0 个答案:

没有答案