如何通过多部门的请求传递参数 [英] How to pass parameter in multipart post request
问题描述
Servlet代码
request.getparameter(fname)//我无法获取值。
HTML代码
< html>
< head>
< title>档案上传表格< / title>
< / head>
< body>
< h3>档案上载:< / h3>
选择要上传的文件:< br />
< form action =UploadServletmethod =post
enctype =multipart / form-data>
< input type =textname =fnamesize =50/>
< input type =filename =filesize =50/>
< input type =submitvalue =Upload File/>
< / form>
< / body>
< / html>
我的问题是:如何传递 fname
参数中的多部分发布请求
简短的回答:在请求的 Part
中找到 fname
。
<对于多部分类型的请求,即使简单的
< input type =text>
字段值也被放置在部分中。你必须迭代由 HttpServletRequest.getParts()
返回的部分
对象,并根据它们的<$ (p:request.getParts()){c $ c> name property:
if(filename.equals(p.getName())) )){
...
}
}
进一步复杂的东西,部分的内容是可用的 InputStream
- Part.getInputStream()
- 所以你将有做一点改造流 byte []
→ 字符串
来获得值。
Servlet code
request.getparameter("fname") //I can't able to get value.
HTML code
<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>
My question is : How to pass fname
parameter in multipart post request?
Short answer: you will find the fname
in the Part
s of the request.
Long answer: For multipart type requests, even the simple <input type="text">
field values are placed in parts. You will have to iterate over the Part
objects returned by HttpServletRequest.getParts()
and handle them according to their name
property:
for( Part p : request.getParts() ) {
if( "fname".equals(p.getName()) ) {
...
}
else if( "file".equals(p.getName()) ) {
...
}
}
To complicate things further, the content of the part is available as InputStream
- Part.getInputStream()
- so you will have to do a little transforming stream → byte[]
→ String
to get the value.
这篇关于如何通过多部门的请求传递参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!