struts1.2中ActionForm和ServletFileUpload.parseReq...

一個問題。在back_add.jsp中把需要保存的內容放在"opinion"中,然後在action中獲取的時候,用request.getParameter("opinion")可以正確獲得值 
   但是用para.get("opinion")就死活取不到值,具體代碼如下:  
Map<String, String> para = new HashMap<String, String>();  
   request.setCharacterEncoding("UTF-8");  
   DiskFileItemFactory factory = new DiskFileItemFactory();  
   ServletFileUpload upload = new ServletFileUpload(factory);  
   try   
   {  
   List items = upload.parseRequest(request);  
   Iterator itr = items.iterator();  
   while (itr.hasNext())   
   {  
   FileItem item = (FileItem) itr.next();  
   if (item.isFormField())   
   {  
   para.put(item.getFieldName(), item.getString("UTF-8"));  
   }  
   else  
   {  
   if (item.getName() != null && !item.getName().equals(""))   
   {  
   int last = item.getName().lastIndexOf("\\");  
   String temString = item.getName().substring(last + 1)+ "/";  
   String read = item.getName().substring(last + 1);  
   String suffix = read.substring(read.lastIndexOf("."));  
   String uuid = java.util.UUID.randomUUID().toString();  
   temString = uuid + suffix + "*" + temString;   
   enclosure = enclosure + temString;   
   File tempFile = new File(item.getName());  
   File file = new File(servlet.getServletContext().getRealPath("/")+ "\\upload\\", uuid + suffix);  
   item.write(file);  
   }  
   }  
   }  
   }   
   catch (Exception e)   
   {  
   e.printStackTrace();  
   }  
這部分代碼還包含了對上傳的附件的處理。到這裏,要用para.get("opinion")就會報空指針異常。  
   一直沒想通,在網上也沒搜到滿意的解決辦法,把用到的方法都拆開看了,也沒看出什麼不妥,一句一句的測試,到upload還貌似正確,但是items怎麼都爲空……  
今天終於在網上看到一個哥們兒答到點子上了~  

原來,ActionForm和ServletFileUpload.parseRequest(request)是不能同時使用的!!!!!!!!!!!!  

   網上匿名高人是這麼解釋的:  
解釋struts用ActionForm的方式處理上傳附件的一些問題,struts接收到enctype="multipart/form-data"的post請求後,會看那個對應的action有沒有配置actionform,如果配置了,就會作一些處理,所以你在action裏得到的request已經不是一個普通的request了,而是一個被封裝過的request。如果想得到原始的request,就不要struts-config.xml裏給action類配置actionform。
ServletFileUpload.parseRequest(request)中的request用的是普通的request,而使用actionForm時request被封裝,從而導致ServletFileUpload.parseRequest(request)取不到值,爲空。目前來說,無法解決ActionForm和ServletFileUpload.parseRequest(request)共存問題,那隻能換別的上傳方式了! 
引用自:http://zhidao.baidu.com/question/196663366.html?fr=qrl&cid=870&index=5
 
爲了這個,不知道花了多少時間……要是早點知道就好了,唉……
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章