从JSP表单中输入类型为文本的值(enctype="multipart/form-data")返回null。

14

我需要上传一张图片:

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

在我的servlet中,我给定了

response.setContentType("text/html");

PrintWriter out = response.getWriter();    

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

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

但是name的值被返回为NULL

请帮忙。


1
相关链接:https://dev59.com/9HE95IYBdhLWcg3wKq2q#2424824 - BalusC
5个回答

11

尝试使用<input type="text" id="name" name="name" value="J.Doe">

编辑:

根据David的答案建议,使用Apache Commons Fileupload示例:

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

我试过了,但是值仍然为空。 - jennifer
1
我只是在投票您的帖子,上上下下地浏览,看看有没有独角兽。 - David O'Meara
@Jennifer,你有解决方案吗?当我尝试使用request.getParameter("name")时,我也得到了null值。如果你有任何解决方案,请告诉我好吗? - Nallamachu

6

尝试

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

使用它需要Apache commons-fileupload.jarcommons-io.jar


5

我使用过的任何容器都不支持开箱即用的多部分编码请求。因此,它无法解析参数并且您不能直接使用request.getParameter()。

您需要在服务器端使用类似于Apache Commons FileUpload的东西来预处理请求。


1
你说得对,我没有看到多部分声明。我会在我的答案中添加我们代码的示例。 - Thomas
那么你从未使用过自2009年12月发布的Servlet 3.0 API吗? - BalusC

1

request.getParameter("name"); 返回的null值是因为在您的HTML表单中使用了enctype="multipart/form-data"

这在此帖中已经得到了详细解答。


0
添加注释@javax.servlet.annotation.MultipartConfig,然后只需简单地使用request.getParameter()即可完美运行。
或者,如果您正在使用MultipartFormDataRequest,则使用它的对象,例如MultipartFormDataRequest mrequest;代替request。例如:mrequest.getParameter("name");它可以正常工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接