雅典之夏的小站
          知人者智 自知者明 Fighting!!

          今天在原來上傳文件頁面的基礎上,想添加一段文件的簡介

          因為同時要上傳文件,所以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.

          按照上面的步驟來,果然一切都ok,GOOGLE真不錯,主要是getFieldName和getString,
          雖然說這種做法有一點麻煩,但稍微判斷加工一下,總比獲取不到強
          posted on 2005-10-27 13:01 rkind 閱讀(860) 評論(0)  編輯  收藏 所屬分類: JSP&ServletJAVA基礎&數據庫
          主站蜘蛛池模板: 伊宁县| 兴仁县| 东宁县| 中牟县| 湘潭市| 衢州市| 襄垣县| 宁陵县| 新丰县| 富蕴县| 祥云县| 娄底市| 博罗县| 安溪县| 溧水县| 锡林浩特市| 聊城市| 花垣县| 方山县| 彰化县| 广安市| 洪洞县| 惠来县| 灵璧县| 楚雄市| 长兴县| 富宁县| 苗栗县| 蒙城县| 泌阳县| 留坝县| 凌源市| 黄山市| 察哈| 胶州市| 桃江县| 察雅县| 自治县| 门源| 德阳市| 宣威市|