ServletFileUpload#parseRequest(request) returns an empty list

亡梦爱人 提交于 2019-11-27 21:39:23
BalusC

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".

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.

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.

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

share.li

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!