Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I read other parameters in a multipart form with Apache Commons

I have a file upload form that is being posted back to a servlet (using multipart/form-data encoding). In the servlet, I am trying to use Apache Commons to handle the upload. However, I also have some other fields in the form that are just plain fields. How can I read those parameters from the request?

For example, in my servlet, I have code like this to read in the uplaoded file:

    // Create a factory for disk-based file items
    FileItemFactory factory = new DiskFileItemFactory();
    // Create a new file upload handler
    ServletFileUpload upload = new ServletFileUpload(factory);
    // Parse the request
    Iterator /* FileItem */ items = upload.parseRequest(request).iterator();
    while (items.hasNext()) {
        FileItem thisItem = (FileItem) items.next();
        ... do stuff ...
    }
like image 807
pkaeding Avatar asked Dec 30 '22 07:12

pkaeding


1 Answers

You could try something like this:

while (items.hasNext()) {
        FileItem thisItem = (FileItem) items.next();
        if (thisItem.isFormField()) {
            if (thisItem.getFieldName().equals("somefieldname") {
                String value = thisItem.getString();
                // Do something with the value
            }
        }

    }
like image 169
stian Avatar answered Jan 07 '23 05:01

stian