Jee*_*uza 5 jsp servlets file-upload multipartform-data
request.getparameter("fname") //I can't able to get value.
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何fname在多部分帖子请求中传递参数?
简短的回答:您会在请求的 sfname中找到。Part
长答案:对于多部分类型请求,即使是简单的<input type="text">字段值也会放在各个部分中。您必须迭代Part返回的对象HttpServletRequest.getParts()并根据其name属性处理它们:
for( Part p : request.getParts() ) {
if( "fname".equals(p.getName()) ) {
...
}
else if( "file".equals(p.getName()) ) {
...
}
}
Run Code Online (Sandbox Code Playgroud)
让事情变得更加复杂的是,该部分的内容可用作InputStream- Part.getInputStream()- 因此您必须进行一些转换流 → byte[]→String才能获取值。