2007-12-17
解决当FORM的ENCTYPE=multipartform-data 时取不到值
在开发一个MIS系统中,部分页面中有需要上传文件的字段,相信大家在开发中也经常遇到这样的情况.因为要上传文件,所以FORM标记中的ENCTYPE="multipart/form-data",可是这样的话,当你在servlet里面用request.getParameter()方法无论如何都只是获得null值,没有办法只好在网上搜索一下,其中收集到了不同的方法,贴出来以备查询.
方法一
用jspsmartupload组件实现文件上传的
这个方法是我使用的方法,所以把主要代码贴了出来
这样就可以取得对应的值了.
方法二
这个是在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.
主要是getFieldName和getString,判断加工一下,还是可以获取到的.
方法三
使用jspsmartupload组件的
只需要在servlet中添加
方法一
用jspsmartupload组件实现文件上传的
这个方法是我使用的方法,所以把主要代码贴了出来
SmartUpload upload = new SmartUpload();
try{
upload.initialize(config, request, response);
// 允许上传的文件类型
upload.setAllowedFilesList("doc,xls,");
// 拒绝上传的文件类型
upload.setDeniedFilesList("exe,bat,jsp");
// 允许上传文件的单个最大大小
upload.setMaxFileSize(1024 * 1024 * 20);
// 允许上传文件的最大大小总和
// upload.setTotalMaxFileSize(1024*1024*10);
//上传数据
upload.upload();
}
catch (SmartUploadException e){
e.printStackTrace();
return;
}
Request req = upload.getRequest();
String spid=(String)req.getParameter("teacherId");
//.....
//To do something
这样就可以取得对应的值了.
方法二
这个是在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.
主要是getFieldName和getString,判断加工一下,还是可以获取到的.
方法三
使用jspsmartupload组件的
只需要在servlet中添加
//中文和日文时使用
request.setCharacterEncoding("UTF-8");
//***************************************************************
JspFactory _jspxFactory = null;
PageContext pageContext = null;
JspWriter out = null;
_jspxFactory = JspFactory.getDefaultFactory();
pageContext = _jspxFactory.getPageContext(this,
request, response,"", true, 8192, true);
out = pageContext.getOut();
//smartupload
SmartUpload su = new SmartUpload();
su.initialize(pageContext);
su.upload();
Request requestSu = su.getRequest();
//getParameter
//普通的
int id = Integer.parseInt(
requestSu.getParameter("id"));
String languages = requestSu.getParameter("languages");
String flag = requestSu.getParameter("flag");
//*******************************************************
//中文和日文时使用
Description = request.getParameter("Description");
//******************************************************
另外在jsp中要传中文和日文得使用
document.form_SuccessfulCase.action=
"/homepage/SuccessfulCase?title=" +
encodeURI(document.form_SuccessfulCase.title.value)+
"&Description="+
encodeURI(document.form_SuccessfulCase.Description.value);
//***********************************
- 11:30
- 浏览 (491)
- 评论 (1)
- 分类: System Security
- 相关推荐
评论
Joo
2008-01-14
当FORM设置了ENCTYPE=multipartform-data之后,POST的request被自动的以stream形式处理(也就是遵从了rfc1867协议标准,在最初的 http 协议中,没有上传文件方面的功能。 rfc1867 (http://www.ietf.org/rfc/rfc1867.txt) 为 http 协议添加了这个功能。客户端的浏览器,如 Microsoft IE, Mozila, Opera 等,按照此规范将用户指定的文件发送到服务器。服务器端的网页程序,如 php, asp, jsp 等,可以按照此规范,解析出用户发送来的文件。Microsoft IE, Mozila, Opera 已经支持此协议,在网页中使用一个特殊的 form 就可以发送文件。)
我用的是commons-fileUpload-1.2的组件+JSF RI 1.2开发,解决方法是直接利用commons中提供的,在上传button的actionLister方法中:
1. 首先用检测是否是一个fileUpload的request
2. 用isFormField()作判断,分离出request中上传文件部分和其他部分分别作处理
参考:
commons-fileUpload官方user guide:
http://commons.apache.org/fileupload/using.html
其他人有关此问题的讨论
http://www.jdon.com/jivejdon/thread/9521.html
我用的是commons-fileUpload-1.2的组件+JSF RI 1.2开发,解决方法是直接利用commons中提供的,在上传button的actionLister方法中:
1. 首先用检测是否是一个fileUpload的request
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
2. 用isFormField()作判断,分离出request中上传文件部分和其他部分分别作处理
参考:
commons-fileUpload官方user guide:
http://commons.apache.org/fileupload/using.html
其他人有关此问题的讨论
http://www.jdon.com/jivejdon/thread/9521.html
发表评论
- 浏览: 11321 次
- 性别:


- 详细资料
搜索本博客
最近加入圈子
最新评论
-
总结JDBC连接SQLServer的 ...
写的很不错
-- by huoxp007 -
解决当FORM的ENCTYPE=mult ...
当FORM设置了ENCTYPE=multipartform-data之后,POS ...
-- by Joo






评论排行榜