开发者

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开发者_如何学Python 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


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.


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

    // ...
}


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜