ServletFileUpload#parseRequest(request)返回一个空的列表 [英] ServletFileUpload#parseRequest(request) returns an empty list

查看:935
本文介绍了ServletFileUpload#parseRequest(request)返回一个空的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android中有一个客户端应用程序,它使用 HttpURLConnection 将文件发送到服务器。服务器使用Apache Commons FileUpload API来解析表单数据值。
$ b

HttpURLConnection 发送这个请求:

  -------------------------- --- 4912995119421 
内容处理:表单数据; name =deviceid

9428103
----------------------------- 4912995119421
Content-Disposition:form-data; name =countryid

598
----------------------------- 4912995119421
Content-Disposition:form-data; name =number

98621360
----------------------------- 4912995119421
Content-Disposition:form-data; NAME = 文件;文件名=2012-12-08 17.42.18.jpg
内容类型:image / jpeg

ÿØÿá1Exif II *

² Ä(1Ì2Øi‡ì%\ n SAMSUNG GT-S5360L HH S5360LUHLB1 2012:12:08 17:42:18?,?????0220???'& '
'。0100 @°>£6¤
2012:12:08 17:42:18 2012:12:08 17:42:18
d R98 0100(¤ T.ÿØÿàJFIF C C C @@ÿÛ
----------------------------- 4912995119421--

服务器代码:

 <$ c $ ()(contentType.indexOf(multipart / form-data)== -1)){
response.setStatus(HttpSe rvletResponse.SC_NOT_FOUND);
return;
}


long maxFileSize =(2 * 1024 * 1024);

int maxMemSize =(2 * 1024 * 1024);

DiskFileItemFactory factory = new DiskFileItemFactory();
//存储在内存中的最大大小
factory.setSizeThreshold(maxMemSize);

//创建一个新的文件上传处理程序
ServletFileUpload upload = new ServletFileUpload(factory);
//要上传的最大文件大小。

upload.setSizeMax(maxFileSize);

List fileItems = upload.parseRequest(request);
Iterator i = fileItems.iterator();


// leo primero todas las变量。

int deviceID = 0;
int countryID = 0;
String phoneNumber =; (i.hasNext()){
FileItem fi =(FileItem)i.next();



while如果(fi.isFormField()){
字符串变量= fi.getFieldName();

if
if(variable.equals(deviceid)){
deviceID = Integer.parseInt(fi.getString());
} else if(variable.equals(countryid)){
countryID = Integer.parseInt(fi.getString());
} else if(variable.equals(number)){
phoneNumber = String.valueOf(Long.parseLong(fi.getString()));





if(deviceID == 0 || countryID == 0 || phoneNumber.equals( )){
response.setStatus(HttpServletResponse.SC_NOT_FOUND);
return;

$ / code>

问题在于 List fileItems = upload .parseRequest(请求);
返回的列表是空的,我无法获取表单数据值。

解决方案

这一点,如果你已经(隐式)预先解析请求的身体。 HTTP请求体只能被读取/解析一次(因为客户端只发送一次,不会多次发送)。



在请求到Commons FileUpload之前调用以下任何一个方法时,请求主体将被隐式读取/解析:

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

您必须确保您 (如果你已经确定你没有这样做,那么唯一的其他可能的原因是一个不正确的边界标题和/或使用不正确的换行符(它确实是CR + LF,因此不是单独的LF)。你可以在使用java.net.URLConnection触发和处理HTTP请求,在上传文件部分下。


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 form-data values.

The HttpURLConnection sends this request:

-----------------------------4912995119421
Content-Disposition: form-data; name="deviceid"

9428103
-----------------------------4912995119421
Content-Disposition: form-data; name="countryid"

598
-----------------------------4912995119421
Content-Disposition: form-data; name="number"

98621360
-----------------------------4912995119421
Content-Disposition: form-data; name="file"; filename="2012-12-08 17.42.18.jpg"
Content-Type: image/jpeg

ÿØÿá1 Exif  II*    
      @      °      ª    
   ²              ¼       Ä   (       1    Ì   2    Ø          i‡    ì   %ˆ    \  n  SAMSUNG GT-S5360L H      H      S5360LUHLB1 2012:12:08 17:42:18  š‚    î  ?‚    ö  "ˆ       'ˆ    È    ?    0220?    þ  ?      ‘     ’    &  ’       
’    .        0100             @       °       >  £       ¤        ¤        ¤    6  ¤                     
   2012:12:08 17:42:18 2012:12:08 17:42:18    
     d               R98      0100                         (           ¤      T.      ÿØÿà JFIF      ÿÛ C @@ÿÛ 
-----------------------------4912995119421--

The server code:

    String contentType = request.getContentType();
    if ((contentType.indexOf("multipart/form-data") == -1)) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        return;
    }


    long maxFileSize = (2 * 1024 * 1024);

    int maxMemSize = (2 * 1024 * 1024);

    DiskFileItemFactory factory = new DiskFileItemFactory();
    // maximum size that will be stored in memory
    factory.setSizeThreshold(maxMemSize);

    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);
    // maximum file size to be uploaded.

    upload.setSizeMax(maxFileSize);

    List fileItems = upload.parseRequest(request);
    Iterator i = fileItems.iterator();


    //leo primero todas las variables.

    int deviceID = 0;
    int countryID = 0;
    String phoneNumber = "";



    while (i.hasNext()) {
        FileItem fi = (FileItem) i.next();

        if (fi.isFormField()) {
            String variable = fi.getFieldName();
            if (variable.equals("deviceid")) {
                deviceID = Integer.parseInt(fi.getString());
            } else if (variable.equals("countryid")) {
                countryID = Integer.parseInt(fi.getString());
            } else if (variable.equals("number")) {
                phoneNumber = String.valueOf(Long.parseLong(fi.getString()));
            }

        }

    }

    if (deviceID == 0 || countryID == 0 || phoneNumber.equals("")) {
        response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        return;
    }

The problem is in the line List fileItems = upload.parseRequest(request);. The returned list is empty and I can not get the form-data values.

解决方案

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

这篇关于ServletFileUpload#parseRequest(request)返回一个空的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆