Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

values of input text fields in a html multipart form

I use Apache Commons FileUpload in a java server-side app that has a html form with fields :

  1. a destination fied that will be filled with email address of the destination mailbox

  2. a message text with a message of the sender

  3. a <input type=file ... field for uploading a photo. I can receive uploaded file (as a stream) but how I can access 1) and 2) form values (completed by the user of app)? Many thanks, Aurel
like image 221
aurel Avatar asked Apr 20 '11 12:04

aurel


People also ask

What does Enctype =' multipart form data mean?

enctype='multipart/form-data' means that no characters will be encoded. that is why this type is used while uploading files to server. So multipart/form-data is used when a form requires binary data, like the contents of a file, to be uploaded.

How do you set a multipart form data?

Multipart form data: The ENCTYPE attribute of <form> tag specifies the method of encoding for the form data. It is one of the two ways of encoding the HTML form. It is specifically used when file uploading is required in HTML form. It sends the form data to server in multiple parts because of large size of file.

What is multipart form data content type?

Multipart/form-data is ideal for sending non-ASCII or binary data, and is the only content type that allows you to upload files. For more information about form data, see http://www.w3.org/TR/html401/interact/forms.html. NOTE: In cURL, you specify each message part using the -F (or --form ) option.

What does Enctype mean?

Definition and Usage The enctype attribute specifies how form-data should be encoded before sending it to the server. The form-data is encoded to "application/x-www-form-urlencoded" by default.


4 Answers

I am guessing you are using a FileItemIterator to iterate the items in the request. The iterators next() method returns a FileItemStream (not a FileItem). Open the stream on that object and turn it into a string like this:

import org.apache.commons.fileupload.util.Streams;
...
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
String name = item.getFieldName();
String value = Streams.asString(stream);

The getString method suggested by other answers is a method on the FileItem interface.

like image 161
pierrovic Avatar answered Oct 26 '22 07:10

pierrovic


You can receive them using the same API. Just hook on when FileItem#isFormField() returns true. If it returns false then it's an uploaded file as you probably already are using.

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        List<FileItem> items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
        for (FileItem item : items) {
            if (item.isFormField()) {
                // Process regular form field (input type="text|radio|checkbox|etc", select, etc).
                String fieldname = item.getFieldName();
                String fieldvalue = item.getString();
                // ... (do your job here)
            } else {
                // Process form file field (input type="file").
                String fieldname = item.getFieldName();
                String filename = FilenameUtils.getName(item.getName());
                InputStream filecontent = item.getInputStream();
                // ... (do your job here)
            }
        }
    } catch (FileUploadException e) {
        throw new ServletException("Cannot parse multipart request.", e);
    }

    // ...
}
like image 43
BalusC Avatar answered Oct 26 '22 06:10

BalusC


Here's what I am using for this purpose:

    public static Hashtable getParamsFromMultipartForm(HttpServletRequest req) throws FileUploadException {
        Hashtable ret = new Hashtable();
        List items = new ServletFileUpload(new DiskFileItemFactory()).parseRequest(req);
        for (FileItem item : items) {
            if (item.isFormField()) {
                ret.put(item.getFieldName(), item.getString());
            }
        }
        return ret;
    }

And then, whenever i need the value of any of my params, i just write, say:

//at the beginning of a servlet
Hashtable multipartParams = TheClassWhereIPutThatMethod.getParamsFromMultipartForm(req);

String myParamFromForm = multipartParams.get("myParamFromForm");

like image 41
Ibolit Avatar answered Oct 26 '22 07:10

Ibolit


So, what I did is to use the instance of fileItem as in:

Hashtable incoming = new Hashtable();
fileName = sfu.parseRequest(request);

//iterating over each uploaded file and storing the values of different parameters in the HashTable (incoming)

for(FileItem f:fileName)
                    {
                    incoming.put(f.getFieldName(), f.getString()); 
                    }
//utilizing that HashTable and getting the value of desired field in the below manner, as in my case i required the value of "permissions" from the jsp page

             for(FileItem f:fileName)
                    {
                        String role= (String)incoming.get("permission"); //as it is a multipart form request, so need to get using this
                    }   

Thanks

like image 42
Yash Bansal Avatar answered Oct 26 '22 07:10

Yash Bansal