【问题标题】:Input TYPE TEXT Value from JSP form (enctype="multipart/form-data") returns null [duplicate]从 JSP 表单输入 TYPE TEXT 值(enctype="multipart/form-data")返回 null [重复]
【发布时间】:2011-07-27 14:44:22
【问题描述】:

我需要上传一张图片:

<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

请帮忙

【问题讨论】:

标签: java jsp servlets multipartform-data


【解决方案1】:

添加注释 @javax.servlet.annotation.MultipartConfig 然后只需使用 request.getParameter() 即可完美运行。

或者,如果您正在使用 MultipartFormDataRequest,则使用它的对象,例如 MultipartFormDataRequest mrequest; 代替请求前。 mrequest.getParameter("名称");它有效。

【讨论】:

    【解决方案2】:

    试试

         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

    【讨论】:

      【解决方案3】:

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

      这个post 已经彻底回答了这个问题。

      【讨论】:

        【解决方案4】:

        试试&lt;input type="text" id="name" name="name" value="J.Doe"&gt;

        编辑:

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

        【讨论】:

        • 我试过了,但值仍然是NULL
        • 我只是在你的帖子上下投票,看看独角兽。
        • @jennifer,你有什么解决方案吗?当我尝试使用 request.getParameter("name") 时,我也得到了 null 值。如果您有任何解决方案,请告诉我?
        【解决方案5】:

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

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

        【讨论】:

        • 你是对的,没有看到多部分声明。我将在我的答案中添加我们的代码示例。
        • 那么自 2009 年 12 月发布的 Servlet 3.0 API 以来,您从未使用过?
        猜你喜欢
        • 2016-05-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多