Alex Rivera | Logout

values of input text fields in a html multipart form

Asked 2011-04-20T12:45:41.213
9

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
Edit
Report

1 Answer

1

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");

answered 2011-11-25T13:08:21.713

Your Answer