现在的位置: 首页 > 综合 > 正文

form表单传递getParameter得不到值

2016年11月17日 ⁄ 综合 ⁄ 共 2714字 ⁄ 字号 评论关闭

jFinal框架下,html 里的一个form表单,post传值,通过Action调用一个方法,enctype="multipart/form-data",最主要是上传文件,编码方式好像非常重要,只有加上此编码方式才可以得到上传来的文件。

目标:随着文件上传还有一两个参数要传递,Action对应的方法里面利用Jfinal框架提供的方法  getPara("name") 得到Parameter ,此方法实质也用的是HttpServeletRequest.getParameter来封装的,查了很多东西,查了一个文档,上面是说用另外一个组件可以解决,

http://blog.csdn.net/georgejin/article/details/1706647

具体内容如下:

I cannot read the submitter using request.getParameter("submitter") (it returns null). ]

Situation:

javax.servlet.HttpServletRequest.getParameter(String) returns null when the ContentType is multipart/form-data

Solutions:

Solution A:

1. download http://www.servlets.com/cos/index.html
2. invoke getParameters() on com.oreilly.servlet.MultipartRequest

Solution B:

1. download http://jakarta.apache.org/commons/sandbox/fileupload/
2. invoke readHeaders() in 
org.apache.commons.fileupload.MultipartStream

Solution C:

1. download http://users.boone.net/wbrameld/multipartformdata/
2. invoke getParameter on 
com.bigfoot.bugar.servlet.http.MultipartFormData

Solution D:

Use Struts. Struts 1.1 handles this automatically.
说是不详细,接着往下看,另一种解决方法
> Solution B:
> 1. download 
> http://jakarta.apache.org/commons/sandbox/fileupload/
> 2. invoke readHeaders() in 
> org.apache.commons.fileupload.MultipartStream

The Solution B as given by my dear friend is a bit hectic and a bit complex :( 
We can try the following solution which I found much simpler (at least in usage).

1. Download one of the versions of UploadFile from http://jakarta.apache.org/commons/fileupload/
2. Invoke parseRequest(request) on org.apache.commons.fileupload.FileUploadBase which returns list of org.apache.commons.fileupload.FileItem objects. 
3. Invoke isFormField() on each of the FileItem objects. This determines whether the file item is a form paramater or stream of uploaded file. 
4. Invoke getFieldName() to get parameter name and getString() to get parameter value on FileItem if it's a form parameter. Invoke write(java.io.File) on FileItem to save the uploaded file stream to a file if the FileItem is not a form parameter. 

还有一种方法就是使用jspsmartupload

表单中enctype="multipart/form-data"的意思,是设置 表单的MIME编码。默认情况,这个编码格式是application/x-www-form-urlencoded,不能用于文件上传;只有使用了 multipart/form-data,才能完整的传递文件数据

但是设置了 enctype="multipart/form-data" ,除了file类型表单能获取到,其他value通过request.getParameter都得不到。这种情况下我们可以利用组件来解决该问题,例如用 jspsmartupload组件

   com.jspsmart.upload.SmartUpload su = new   com.jspsmart.upload.SmartUpload();
   su.initialize(pageContext);
   su.service(request, response);
   su.setTotalMaxFileSize(100000000);
   su.setAllowedFilesList("zip,rar");
   su.setDeniedFilesList("exe,bat,jsp,htm,html,,");
   su.upload();

   String Name = su.getRequest().getParameter("Name");
   String TYPE_ID = su.getRequest().getParameter("Type");

通过 su.getRequest().getParameter("value");就可以了,su.upload()好象必须放在前面,测试中将su.upload()放在获取参数后面不成功。

对于我的代码,我想应该不用这么复杂,我偶然把自己获取参数的方法放到了获取文件的代码之后,然后奇迹出现了,就可以获取,我想,这是跟编码方式主要是依据文件来的,可能整个表单的传递是以文件流为主的,其他参数附着其之后,必须在文件流之后获得参数才行。

抱歉!评论已关闭.