Passing parameters along with a multipart/form-data upload form (Java Http Post Upload)

前端 未结 3 855
再見小時候
再見小時候 2020-12-18 09:30

I have a code base which currently uploads file using Post and has enctype as multipart/form-data. Now I need to include some form items i.e. some parameters will also be pa

相关标签:
3条回答
  • 2020-12-18 09:32

    To send a parameter with a FileUpload it just needs to be added in the URL within the setAction method As follows:

    formPanel.setAction("<ProjectURL>/<YourServletName>?<YourParameterName>="+parameter);

    And in your servlet simply get the parameter as follows:

    req.getParameter("<YourParameterName>");

    Hope it helps ;-)

    0 讨论(0)
  • 2020-12-18 09:34

    Similar solutions:

    FileItemStream item = iter.next();
    if(item.isFormField()) {
        String value = item.getString();
    }
    

    or

    FileItemStream item = iter.next();
    if(item.isFormField()) {
       InputStream name = item.getInputStream();
       String value = Streams.asString(name);
    }
    
    0 讨论(0)
  • 2020-12-18 09:50

    If isFormField on FileItemStream returns true it's a normal field. You can use openStream and read the contents into a String.

    Something like this:

    FileItemStream item = iter.next();
    if(item.isFormField()) {
       // Normal field
       String name = item.getFieldName();
       String value = Streams.asString(item.openStream());
    } else {
       // File
    }
    

    Streams.asString takes a second parameter which is the charset encoding to use, you might need to specify one that is suitable for your site.

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