0

How to send multipart data and text data from HTML form to JSP page at time

my form field are Name ,upload file

I am unable to send both at a time

ajm
  • 12,863
  • 58
  • 163
  • 234
raju
  • 7
  • 3

1 Answers1

0

In a multipart/form-data request, the text parameter is also sent as a multipart item, not as a regular query parameter. To get the text parameter, you need to use the same API as you have used to get the uploaded file. Assuming that you're using Apache Commons FileUpload, which is a de facto standard multipart/form-data parser, then you need to hook on the condition where the item is a normal form field.

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

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555