我需要上传图片:
<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。
name
NULL
请帮助
尝试<input type="text" id="name" name="name" value="J.Doe">。
<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(); } }