values of input text fields in a html multipart form

前端 未结 4 2276
走了就别回头了
走了就别回头了 2020-12-17 23:47

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 t

相关标签:
4条回答
  • 2020-12-17 23:55

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

    0 讨论(0)
  • 2020-12-18 00:11

    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);
        }
    
        // ...
    }
    
    0 讨论(0)
  • 2020-12-18 00:13

    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 讨论(0)
  • 2020-12-18 00:14

    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.

    0 讨论(0)
提交回复
热议问题