`
geshenyi
  • 浏览: 101005 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

解决当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);    
//***********************************
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics