打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
解决当FORM的ENCTYPE="multipart/form-data" 时reques...

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

因为同时要上传文件,所以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-dataSolutions:Solution A:1. download http://www.servlets.com/cos/index.html2. invoke getParameters() on com.oreilly.servlet.MultipartRequestSolution B:1. download http://jakarta.apache.org/commons/sandbox/fileupload/2. invoke readHeaders() inorg.apache.commons.fileupload.MultipartStreamSolution C:1. download http://users.boone.net/wbrameld/multipartformdata/2. invoke getParameter oncom.bigfoot.bugar.servlet.http.MultipartFormDataSolution 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.

按照上面的步骤来,果然一切都ok,GOOGLE真不错,主要是getFieldName和getString,
虽然说这种做法有一点麻烦,但稍微判断加工一下,总比获取不到强
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
基于表单的文件上传
CMS项目总结:18、文件上传commons
Apache commons FileUpload 组件
关于httpclient中MultipartPostMethod类上传文件的一点感受
Servlet 文件上传 | 菜鸟教程
SpringMVC上传文件的三种方式
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服