ServletFileUpload#parseRequest(request) returns an empty list

前端 未结 5 1420
夕颜
夕颜 2020-12-05 20:57

I have a client application in Android which uses HttpURLConnection to send files to the server. The server uses the Apache Commons FileUpload API to parse the

相关标签:
5条回答
  • 2020-12-05 21:32

    It will be empty at that point if you have already (implicitly) parsed the request body beforehand. The HTTP request body can be read/parsed only once (as the client has sent it only once and won't send it multiple times).

    The request body will be implicitly read/parsed when you have invoked any of the following methods before feeding the request to Commons FileUpload:

    request.getParameter();
    request.getParameterMap();
    request.getParameterNames();
    request.getParameterValues();
    request.getReader();
    request.getInputStream();
    

    You need to make absolutely sure that you are not calling any of those methods beforehand (also check all servlet filters to be sure).

    If you've already ensured that you aren't doing that, then the only other possible causes would be an incorrect boundary header and/or using incorrect newlines (it has really to be CR+LF and thus not alone LF). You can find a concrete and proper example at the bottom of Using java.net.URLConnection to fire and handle HTTP requests, under the section "Uploading files".

    0 讨论(0)
  • 2020-12-05 21:41

    I was running into this problem as well, but I couldn't find anything that specifically related to reading the request object. I finally got it to work by removing this block:

    @MultipartConfig(
        location="/tmp", 
        fileSizeThreshold=1024*1024, 
        maxFileSize=1024*1024*5, 
        maxRequestSize=1024*1024*5*5
    )
    

    Apparently when I had been trying to read the request by using getParts(), I forgot to remove this. I'm a bit new to java, so I didn't know this conflicts with the ServletFileUpload#parseRequest. Anyway, once I got rid of it, the parseRequest correctly returned the array.

    0 讨论(0)
  • 2020-12-05 21:43

    If spring is used, check if there is a <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" /> in xml file. There is a function

    protected HttpServletRequest checkMultipart(HttpServletRequest request) throws MultipartException {
            if (this.multipartResolver != null && this.multipartResolver.isMultipart(request)) {
                if (request instanceof MultipartHttpServletRequest) {
                    logger.debug("Request is already a MultipartHttpServletRequest - if not in a forward, " +
                            "this typically results from an additional MultipartFilter in web.xml");
                }
                else {
                    return this.multipartResolver.resolveMultipart(request);
                }
            }
            // If not returned before: return original request.
            return request;
        }
    

    "eats" the multipart items. It causes a three hours pain.

    0 讨论(0)
  • 2020-12-05 21:49

    my problem is apache context.xml on my linux server contains Context

    allowCasualMultipartParsing="true" and my HttpServlet contains

    @MultipartConfig(fileSizeThreshold=1024*1024*2,maxFileSize=1024*1024*50,maxRequestSize=1024*1024*50)
    

    I delete it then it's working correctly. Hope this will help you.

    0 讨论(0)
  • 2020-12-05 21:55

    I had this same problem after trying I find that it was incompatibility of libraries, library apache server with fileupload library, so I remove the library and imported from the library server Apache.

    imports:
    import org.apache.tomcat.util.http.fileupload.FileItem;
    import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
    import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
    

    sorry my english

    0 讨论(0)
提交回复
热议问题