如何通过多部门的请求传递参数 [英] How to pass parameter in multipart post request

查看:180
本文介绍了如何通过多部门的请求传递参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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 Parts 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆