struts2 檔案上傳

來源:互聯網
上載者:User

標籤:style   blog   http   io   ar   color   os   使用   sp   

網上有很多多檔案上傳的資料,我只記錄需要注意的一點:

jsp頁面:

<div><font size="10">檔案上傳</font><form action="FileUp" method="post" enctype="multipart/form-data"><s:file name="upload" size="20"></s:file><input type="submit" value="上傳"></form>    </div>

 

s標籤中file的name為upload,那麼action中的bean屬性就是upload

import java.io.File;import java.text.SimpleDateFormat;import java.util.Date;import org.apache.commons.io.FileUtils;import org.apache.struts2.ServletActionContext;//需要commons-io包和commons-fileupload包private File upload;//檔案名稱private String uploadFileName;//設定上傳檔案的檔案名稱    注意命名規範 "upload"+"FileName"private String uploadContentType;//上傳檔案的類型    注意命名規範 "upload"+"ContentType"//然後就是執行方法了:public String execute() throws Exception {//System.out.println(upload.getName());//System.out.println(uploadFileName);//System.out.println(uploadContentType);if(upload!=null&&upload.getName()!=null&&!"".equals(upload.getName())){String savePath = "uploads";//上傳到伺服器的哪個檔案夾下,這個也可以在xml檔案中配置String realPath = ServletActionContext.getServletContext().getRealPath("/");savePath = realPath + savePath +"/"+DateUtils.dateFormat(new Date(), "yyyyMMddhhmmss")+ uploadFileName;File saveFile = new File(savePath);if(saveFile.getParentFile()!=null&&!saveFile.getParentFile().exists()){saveFile.getParentFile().mkdirs();//如果父目錄不存在則建立}FileUtils.copyFile(upload, saveFile);this.httpReq.setAttribute("uploadMessage", "上傳成功!");//不要寫upload.message 會把message當做屬性return "SUCCESS";}return "UpFailure";}

 

只要action中屬性名稱對了基本上就能接收到上傳的檔案名稱,當然不要忘了get,set方法

多檔案上傳:

<div><font size="10">檔案上傳</font><form action="FilesUp" method="post" enctype="multipart/form-data"><s:file name="file" size="20"></s:file><br><s:file name="file" size="20"></s:file><br><s:file name="file" size="20"></s:file><br><input type="submit" value="上傳"></form>    </div>private List<File> file;private List<String> fileFileName;private List<String> fileContentType;@Overridepublic String execute() throws Exception {if(file!=null){if(file.size()==0){this.log.info("空檔案上傳!");}else{List<String> filesName = new ArrayList<String>();for (int i=0;i<file.size();i++) {if(file.get(i)!=null&&file.get(i).getName()!=null&&!"".equals(file.get(i).getName())){File saveFile = new File(ServletActionContext.getServletContext().getRealPath("/")+"uploads"+"/"+fileFileName.get(i));FileUtils.copyFile(file.get(i), saveFile);filesName.add(fileFileName.get(i));}else{this.log.info("第"+i+"個檔案為空白,無法上傳!");}}this.httpReq.setAttribute("upFileMessage", filesName);return "SUCCESS";}}return "re";}

 

最後設定檔:

<!-- 指定允許上傳的檔案最大位元組數。預設值是2097152(2M) --><constant name="struts.multipart.maxSize" value="10701096"/><!-- 設定上傳檔案的臨時檔案夾,預設使用javax.servlet.context.tempdir  檔案上傳時使用--><constant name="struts.multipart.saveDir " value="/tmp" />

推薦內容:Struts2檔案上傳原理及樣本
http://blog.sina.com.cn/s/blog_87720b870100zkn2.html

 

struts2 檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.