解决当FORM的ENCTYPE="multipart/form-data" 时request.getParameter()获取不到值的方法

今天在原来上传文件页面的基础上,想添加一段文件的简介

因为同时要上传文件,所以ENCTYPE="multipart/form-data" 必须要加在form里面

可是这样的话,我再servlet里面用request.getParameter()方法无论如何都只是获得null值,

不是一般的郁闷,百度了一下,有人出现了同样的问题可是它用的是jspsmartupload组件实现文件上传的,

而我用的commons fileupload组件,仔细看了一下这个组件的api,可是英语太差了,没有发现相关的信息

我又尝试用session传递参数,可是发现有点麻烦,因为在表单提交之时你就得赋给session表单上它的数值,

这似乎要javascript,可是偶也不会,

后来只有google了,搜索了一些中文网页,也没有找到资料,试试不限制语言,呵呵呵,一大片,后来被俺发

现了这个

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()放在获取参数后面不成功。

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值