java apache。平民fileuploads在一次解析后不解析请求
我正在提交带有文本和文件类型输入字段的表单,并使用此代码获取文本数据
但问题是
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (item.isFormField()) {
// Process normal fields here.
//Taking all text and doing task
System.out.println("Field name: " + item.getFieldName());
System.out.println("Field value: " + item.getString());
} else {
// Process <input type="file"> here.
//And Leaving this at this time
}
}
如果我解析请求并逐个迭代,然后在formField中,我用来获取所有文本参数,然后在文件类型条件下再次使用此代码上载文件,这样它就不会再次解析
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (item.isFormField()) {
// Process normal fields here.
//Leaving this section this time
} else {
// Process <input type="file"> here.
//Use to Upload file
System.out.println("Field name: " + item.getFieldName());
System.out.println("Field value (file name): " + item.getName());
}
}
那么为什么会发生这样的事。。。这个问题的解决方案应该是什么
# 1 楼答案
HTTP请求只能解析一次,因为客户端只发送了一次。在第一次解析过程中,HTTP请求已被完全使用。在对同一请求进行任何后续解析尝试期间,它不再可用
如果你想对它进行两次解析,那么客户端基本上需要发送两次。然而,你不能要求/期望客户这样做,这毫无意义。只需对其进行一次解析,然后为您的具体功能需求寻找不同的解决方案。例如,对两个循环重复使用相同的
items
列表请注意,这基本上是低效的代码。所以,我要重新考虑一下您的功能需求