1

I have a JSP containing a form

        <form action="upload" method="post" enctype="multipart/form-data">
        <fieldset>
            <input name="nom" class="input-xlarge focused" id="nom" type="text" value=""> 
            <input name="date" class="input-xlarge focused" id="date" type="text" value="">
            <input type="file" name="file" /> 
            <button type="submit" class="btn btn-primary">Envoi</button>
        </fieldset>
    </form>

which contains 2 fields (nom and date) and also asking for a file to be uploaded on the server.

on the servlet side, I have the following:

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String UPLOAD_DIRECTORY = request.getServletContext().getRealPath("/")+"imgs/";
    //process only if its multipart content
    if(ServletFileUpload.isMultipartContent(request)){

        String nom = request.getParameter("nom");
        String date = request.getParameter("date");
        log.debug("upload parameters: "+nom+" "+date);


        try {
            List<FileItem> multiparts = new ServletFileUpload(
                                     new DiskFileItemFactory()).parseRequest(request);

            for(FileItem item : multiparts){
                if(!item.isFormField()){
                    String name = new File(item.getName()).getName();
                    item.write( new File(UPLOAD_DIRECTORY + File.separator + name));
                }
            }

           //File uploaded successfully
           request.setAttribute("message", "File Uploaded Successfully");
           log.debug("File updated successfully");
        } catch (Exception ex) {
           request.setAttribute("message", "File Upload Failed due to " + ex);
           log.debug("File upload failed: "+ex);
        }          

    }else{
        request.setAttribute("message",
                             "Sorry this Servlet only handles file upload request");
        log.debug("file upload only !");
    }

    //request.getRequestDispatcher("/result.jsp").forward(request, response);

}

file upload is working properly but I'm not able to retrieve my two parameters (nom and date) by using request.getParameter.

Can I retrieve parameters in a multipart/form-data ? how can I do that ?

tiamat
  • 879
  • 2
  • 12
  • 35

1 Answers1

6

While using enctype="multipart/form-data" you can not directly get parameters by using request.getParameter("nom");.

In this case the form fields aren't available as parameter of the request, they are included in the stream, so you need to get them from stream. A possible way is to use commons-fileupload. Here is sample code from official documentation ( Refer to 'Processing the uploaded items' section)

// Process the uploaded items
Iterator<FileItem> iter = items.iterator();
while (iter.hasNext()) {
    FileItem item = iter.next();

    if (item.isFormField()) {
        processFormField(item);
    } else {
        processUploadedFile(item);
    }
}

For a regular form field

 // Process a regular form field if (item.isFormField()) {
     String name = item.getFieldName();
     String value = item.getString();
     ... }
Sheetal Mohan Sharma
  • 2,908
  • 1
  • 23
  • 24