一尘不染

来自JSP表单的输入TYPE TEXT值(enctype =“ multipart / form-data”)返回null

jsp

我需要上传图片:

<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

请帮助


阅读 288

收藏
2020-06-08

共1个答案

一尘不染

尝试<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();
  }
}
2020-06-08