開發日誌:struts2使用commons.fileupload上傳附件,並解決upload.parseRequest(request)為空白的問題

來源:互聯網
上載者:User

標籤:

要做一個phongap開發的App上傳檔案到伺服器的Action,打算使用commons.fileupload的方式

介面jsp頁面

<form  action="uploadAction.action" method="post" enctype="multipart/form-data"><table><tr><td>上傳附件:uploadAction.action</td></tr><tr><td><input name="file" type="file" size="20" ></td><td>上傳圖片</td></tr><tr><td colspan="2" align="center"><input type="submit" value="上傳"/></td></tr></table></form>


儲存附件的Action方法

/** * 初始化目錄(如果目錄不存在,建立) * @param uploadPath 儲存路徑 * @param tempPath 臨時檔案路徑 * @throws ServletException */private void initDir(String uploadPath,String tempPath) throws ServletException{File uploadFile = new File(uploadPath);if (!uploadFile.exists()) {uploadFile.mkdirs();}File tempPathFile = new File(tempPath);if (!tempPathFile.exists()) {tempPathFile.mkdirs();}}public String uploadAction()throws Exception{try {Date date = new Date();//上傳檔案request.setCharacterEncoding("UTF-8");DiskFileItemFactory factory = new DiskFileItemFactory();factory.setSizeThreshold(4096); // 設定緩衝區大小,這裡是4kbString basePath = ServletActionContext.getRequest().getRealPath("/");//擷取根目錄//設定下載目錄String uploadPath = basePath + "/upload";//設定臨時目錄String tempPath = uploadPath +"/temp";//uploadPath = uploadPath +"/"+ date.getTime();//添加時間目錄防止重複this.initDir(uploadPath, tempPath);File tempPathFile = new File(tempPath);        factory.setRepository(tempPathFile);// 設定緩衝區目錄ServletFileUpload upload = new ServletFileUpload(factory);//upload.setSizeMax(34194304); 設定最大檔案尺寸,4MBList items = upload.parseRequest(request);//得到所有的檔案Iterator itr = items.iterator();while(itr.hasNext()){FileItem fileItem = (FileItem) itr.next();if(fileItem.isFormField()){System.out.println("表單參數名:"+fileItem.getFieldName()+",表單參數值:"+fileItem.getString("utf-8"));}else{if(fileItem.getName()!=null && !"".equals(fileItem.getName())){System.out.println("上傳檔案的大小:"+fileItem.getSize());System.out.println("上傳檔案的類型:"+fileItem.getContentType());System.out.println("上傳檔案的名稱:"+fileItem.getName());//儲存檔案File saveFile = new File(uploadPath,fileItem.getName());fileItem.write(saveFile);imgUrl = uploadPath+"/"+fileItem.getName();System.out.println("上傳成功");}else{System.out.println("沒有上傳檔案");}}}return "success";} catch (Exception e) {e.printStackTrace();throw new Exception(e);}}

調用Action的時候發現
List items = upload.parseRequest(request);

items的值一直為空白,網上找了下原因:參考http://blog.csdn.net/qq964166471/article/details/21385041

原因是struts2把原始的原來S2為簡化上傳功能,把所有的enctype="multipart/form-data"表單做了wrapper最後把HttpServletResquest封裝成 org.apache.struts2.dispatcher.multipart.MultiPartRequestWrapper


我採用的解決步驟

建立MyRequestParser類,重寫parse方法,裡面內容為空白

public class MyRequestParser extends JakartaMultiPartRequest{@Overridepublic void parse(HttpServletRequest servletRequest, String saveDir) throws IOException {}}

然後在struts下增加下面內容

<struts><bean type="org.apache.struts2.dispatcher.multipart.MultiPartRequest"   name="myRequestParser" class="com.MyRequestParser"   scope="default" optional="true"/><constant name="struts.multipart.handler" value="myRequestParser" />  


重啟tomcat後上傳成功


開發日誌:struts2使用commons.fileupload上傳附件,並解決upload.parseRequest(request)為空白的問題

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.