i am submitting form with text and file types input fields and using this code to get text data
but the problem is that
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
}
}
if i parses the request and iterate it from one BY one and then in formField i used to get all text parameters and after it i again used this code in file type condition to upload file so it doesn't parse again
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());
}
}
so why its happening...and what should be the solution for this...????
A HTTP request can be parsed only once because the client has sent it only once. The HTTP request is already consumed in its entirety during the first parse. It is not available anymore during any subsequent parse attempt on the same request.
It you want to parse it twice, then the client has basically to send it twice. However, you can't ask/expect a client to do that, that makes no utter sense. Just parse it once and look for a different solution for your concrete functional requirement. E.g. reuse the same items
list for both loops.
List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
for (FileItem item : items) {
if (item.isFormField()) {
// Process normal fields here.
}
}
for (FileItem item : items) {
if (!item.isFormField()) {
// Process file fields here.
}
}
Note that this is basically inefficient code. So, I'd rethink twice about your functional requirement.