Question

I am using apache commons file upload 1.1 . Currently i am using parseRequest(request) to parse the items from the request .

Now i have an additional request to upload a file . something like default file if user doesn't upload any.

Is that possible ?

Thanks in advance

Was it helpful?

Solution

parseRequest returns a list of FileItems. So, when the list is empty there was no file uploaded.

Therefore, you just need to test if the list is empty. Taking an example from Using FileUpload

ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(request);
if (items.isEmpty()) {
    // process some default file
} else {
    // process uploaded file
}

Update:

According to Processing the uploaded items, you can have files and regular form fields mixed in the request. You can iterate through the parameters, set a flag when you see a file upload, and act accordingly afterwards

// Process the uploaded items
boolean fileUploaded = false;
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
    FileItem item = iter.next();

    if (item.isFormField()) {
        processFormField(item);
    } else {
        processUploadedFile(item);
        fileUploaded = true;
    }
}

if (!fileUploaded) {
    // process some default file
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top