1. 程式人生 > >解決當FORM的ENCTYPE="multipart/form-data" 時request.getParameter()獲取不到值的問題

解決當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()放在獲取引數後面不成功。