(27) java web的struts2架構的使用-基於表單的多檔案上傳

來源:互聯網
上載者:User

標籤:struts   for   檔案大小   沒有   BMI   object   private   app   多檔案   

和單個檔案上傳配置都是一樣的,只是在action中接受參數時候,接受的是數組,不再是單個的檔案。

一,action的實現:

public class MutableFilesUpload extends ActionSupport {private static final long serialVersionUID = 1L;public File[] uploadObjects;public String[] uploadObjectsFileName;public String[] uploadObjectsContentType;public String moreInfo;public String upload() {System.out.println(moreInfo);// 1.拿到ServletContextServletContext servletContext = ServletActionContext.getServletContext();// 2.調用realPath方法,擷取根據一個虛擬目錄得到的真實目錄String realPath = servletContext.getRealPath("/files");// 3.如果這個真實的目錄不存在,需要建立File file = new File(realPath);if (!file.exists()) {file.mkdirs();}// 4.把檔案存過去// 剪下:把臨時檔案剪下指定的位置,並且給他重新命名。 注意:臨時檔案沒有了for (int i = 0; i < uploadObjects.length; i++) {System.out.println("one file name: " + uploadObjectsFileName[i]);uploadObjects[i].renameTo(new File(file, uploadObjectsFileName[i]));}return SUCCESS;}}

  

二,配置action

<action name="mutable_uploads" class="gy.upload.MutableFilesUpload" method="upload">  <!-- 指定(限制)上傳檔案的類型,定義局部攔截器,修改預設攔截器的屬性 "fileUpload.maximumSize" :限制上傳最大的檔案大小。 "fileUpload.allowedTypes":允許上傳檔案的類型。 "fileUpload.allowedExtensions":允許上傳檔案的可擴充檔案類型。 -->  <interceptor-ref name="defaultStack">    <param name="fileUpload.maximumSize">500000000</param>    <param name="fileUpload.allowedTypes">text/plain,application/vnd.ms-powerpoint</param>    <param name="fileUpload.allowedExtensions">.txt,.ppt</param>  </interceptor-ref>  <result>/success.html</result>  <!-- 出現錯誤自動會返回input結果,進入結果檢視 -->  <result name="input">/fail.html</result></action>

 

三,表單的實現

<body><form action="mutable_uploads" method="post"enctype="multipart/form-data">檔案:<br> <input type="file" name="uploadObjects"><br><input type="file" name="uploadObjects"><br> more info:<input type="text" name="moreInfo"><br><input type="submit" value="提交"><br></form></body>

  

  

(27) java web的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.