Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Input TYPE TEXT Value from JSP form (enctype="multipart/form-data") returns null [duplicate]

I need to upload an image:

<form method="post" action="hi.iq/register.jsp" enctype="multipart/form-data">
    Name: <input type="text" name="name" value="J.Doe">
    file: <input type="file" name="file-upload">
    <input type="submit">
</form> 

In my servlet I gave

response.setContentType("text/html");

PrintWriter out = response.getWriter();    

String name = request.getParameter("name");

System.out.println("user_id========= "+name);

but the value of name is returned as NULL.

Pls Help

like image 662
jennifer Avatar asked Apr 01 '11 11:04

jennifer


People also ask

What is the use of Enctype multipart form data in JSP?

Multipart form data: The ENCTYPE attribute of <form> tag specifies the method of encoding for the form data. It is one of the two ways of encoding the HTML form. It is specifically used when file uploading is required in HTML form. It sends the form data to server in multiple parts because of large size of file.

How can we get values from multipart form data in JSP?

String textboxValue = request. getParameter( "textBox1" ); But I always get null value by using this method.

What does Enctype multipart form data mean?

enctype(ENCode TYPE) attribute specifies how the form-data should be encoded when submitting it to the server. multipart/form-data is one of the value of enctype attribute, which is used in form element that have a file upload. multi-part means form data divides into multiple parts and send to server.

What is Enctype form tag?

The enctype attribute specifies how the form-data should be encoded when submitting it to the server. Note: The enctype attribute can be used only if method="post" .


2 Answers

Try <input type="text" id="name" name="name" value="J.Doe">.

Edit:

A sample using Apache Commons Fileupload, as suggested by David's answer:

FileItemFactory factory = new DiskFileItemFactory();

// Set factory constraints
// factory.setSizeThreshold(yourMaxMemorySize);
// factory.setRepository(yourTempDirectory);

// Create a new file upload handler
ServletFileUpload upload = new ServletFileUpload( factory );
// upload.setSizeMax(yourMaxRequestSize);

// Parse the request
List<FileItem> uploadItems = upload.parseRequest( request );

for( FileItem uploadItem : uploadItems )
{
  if( uploadItem.isFormField() )
  {
    String fieldName = uploadItem.getFieldName();
    String value = uploadItem.getString();
  }
}
like image 108
Thomas Avatar answered Oct 08 '22 11:10

Thomas


Try

     FileItemFactory factory = new DiskFileItemFactory();
     ServletFileUpload upload = new ServletFileUpload(factory);
     Iterator<FileItem> iterator = upload.parseRequest(request).iterator();
     File uploadedFile;
     String dirPath="D:\fileuploads";
     while (iterator.hasNext()) {

                    FileItem item = iterator.next();
                    if (!item.isFormField()) {

                        String fileNameWithExt = item.getName();

                        File filePath = new File(dirPath);

                        if (!filePath.exists()) {
                            filePath.mkdirs();
                        }

                        uploadedFile = new File(dirPath + "/" + fileNameWithExt);
                        item.write(uploadedFile);                  
                    }
                    else {
            String otherFieldName = item.getFieldName();
            String otherFieldValue = item.getString()
                    }
               }

It needs Apache commons-fileupload.jar and commons-io.jar

like image 22
Hardik Mishra Avatar answered Oct 08 '22 11:10

Hardik Mishra