解决当FORM的ENCTYPE=multipart/form-data 时取不到值

在开发一个MIS系统中,部分页面中有需要上传文件的字段,相信大家在开发中也经常遇到这样的情况.因为要上传文件,所以FORM标记中的ENCTYPE="multipart/form-data",可是这样的话,当你在servlet里面用request.getParameter()方法无论如何都只是获得null值,没有办法只好在网上搜索一下,其中收集到了不同的方法,贴出来以备查询.

方法一
用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
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);
//***********************************
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
解决enctype= multipart/form-data无法传递其他参数的方法有多种。 一种常见的解决方法是使用一个隐藏的表单字段将额外的参数传递到服务器端。在form标签中添加一个隐藏的input标签,将需要传递的参数作为该字段的。在提交表单时,该参数会随着文件一起被发送到服务器端。 另一种方法是使用JavaScript来动态地修改表单的action属性,并将额外的参数作为查询字符串添加到action中。当用户点击提交按钮时,JavaScript会通过监听表单的提交事件,在提交之前修改action属性并将额外参数添加到其中。这样,提交表单时就会将所有参数一起发送到服务器端。 如果使用的是Ajax来提交表单,可以将其他参数组成一个对象,然后使用FormData对象来存储文件和其他参数。FormData对象可以通过append()方法将文件和其他参数添加到其中,然后通过XmlHttpRequest对象将FormData对象发送到服务器端。 还有一种解决方法是使用服务器端的处理程序来解析multipart/form-data类型的请求,然后从请求体中解析出文件和其他参数。可以根据服务器端的开发语言和框架来选择合适的处理程序,例如在PHP中可以使用$_FILES数组来获取文件,使用$_POST数组来获取其他参数。 综上所述,通过隐藏字段、JavaScript、FormData对象或服务器端的处理程序,都可以实现在enctype= multipart/form-data类型的表单中传递其他参数。选择哪种方法取决于具体的需求和技术环境。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值