Servlet代码 request.getparameter("fname") //I can't able to get value. HTML代码 html head titleFile Uploading Form/title /head body h3File Upload:/h3 Select a file to upload: br / form action="UploadServlet" method="post" enctype="multi
request.getparameter("fname") //I can't able to get value.
HTML代码
<html> <head> <title>File Uploading Form</title> </head> <body> <h3>File Upload:</h3> Select a file to upload: <br /> <form action="UploadServlet" method="post" enctype="multipart/form-data"> <input type="text" name="fname" size="50" /> <input type="file" name="file" size="50" /> <input type="submit" value="Upload File" /> </form> </body> </html>
我的问题是:如何在多部分帖子请求中传递fname参数?
简短回答:您将在请求的部分中找到fname.答案很长:对于多部分类型请求,即使是简单的< input type =“text”>字段值放在部分中.您将不得不迭代HttpServletRequest.getParts()返回的Part对象,并根据其name属性处理它们:
for( Part p : request.getParts() ) { if( "fname".equals(p.getName()) ) { ... } else if( "file".equals(p.getName()) ) { ... } }
为了使事情进一步复杂化,部件的内容可用作InputStream – Part.getInputStream() – 因此您必须执行一些转换流→byte []→String来获取值.