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

牧云@^-^@ 提交于 2019-12-03 02:07:36

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

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

No container that I have used supports multipart encoded requests out of the box. Due to this, it cannot parse parameters and you cannot use request.getParameter() out of the box.

You need to use something on the server side like Apache Commons FileUpload to preprocess the request

braindead

The null value returned by the request.getParameter("name"); is due to the fact that you are using enctype="multipart/form-data" in your html form.

This has been thoroughly answered in this post.

Add Annotaion @javax.servlet.annotation.MultipartConfig and then just simply use request.getParameter() it will work Perfectly.

Or If you are using MultipartFormDataRequest then use its object Like MultipartFormDataRequest mrequest; in place of request for ex. mrequest.getParameter("name"); It works.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!