我在Android中有一个客户端应用程序,它使用HttpURLConnection
将文件发送到服务器。服务器使用Apache Commons FileUpload API来解析表单数据值。
HttpURLConnection
发送此请求:
-----------------------------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--
服务器代码:
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;
}
问题在于List fileItems = upload.parseRequest(request);
行。
返回的列表为空,我无法获取表单数据值。
答案 0 :(得分:26)
如果您已经(隐式)预先解析了请求主体,那么它将是空的。 HTTP请求正文只能被读取/解析(因为客户端只发送一次并且不会多次发送它)。
在将请求提交给Commons FileUpload之前调用以下任何方法时,将隐式读取/解析请求正文:
request.getParameter();
request.getParameterMap();
request.getParameterNames();
request.getParameterValues();
request.getReader();
request.getInputStream();
您需要确保事先不调用任何这些方法(同时检查所有servlet过滤器)。
如果你已经确定你没有这样做,那么唯一的其他可能原因是不正确的边界标题和/或使用不正确的换行符(它实际上是CR + LF,因此不是单独的LF) 。您可以在Using java.net.URLConnection to fire and handle HTTP requests底部的“上传文件”部分下找到具体而恰当的示例。
答案 1 :(得分:1)
我尝试后发现它与库,库apache服务器和fileupload库不兼容,我遇到了同样的问题,所以我删除了库并从库服务器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;
抱歉,我的英文
答案 2 :(得分:1)
如果使用spring,请检查xml文件中是否有<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />
。有一个功能
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;
}
“吃”多部分物品。它会导致三个小时的疼痛。
答案 3 :(得分:0)
我也遇到了这个问题,但是找不到与读取请求对象有关的任何内容。我终于通过删除这个块来实现它:
@MultipartConfig(
location="/tmp",
fileSizeThreshold=1024*1024,
maxFileSize=1024*1024*5,
maxRequestSize=1024*1024*5*5
)
显然当我尝试使用getParts()读取请求时,我忘了删除它。我对java有点新,所以我不知道这与ServletFileUpload #parseRequest有冲突。无论如何,一旦我摆脱它,parseRequest正确地返回了数组。
答案 4 :(得分:-1)
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.