Struts2中實現多檔案上傳於下載

來源:互聯網
上載者:User

做web開發,經常要實現檔案的上傳功能,Struts2使用的是jakarta中的commons-fileupload-1.2.jar和commons-io-1.3.1.jar來實現檔案上傳的,所以必須將這兩個包放入lib目錄

 

實現檔案上傳必須將表單的提交方式設為post(預設為get),並將enctype設定為multipart/form-data.不然無法完成檔案上傳

那麼現在咋們就開始實現檔案上傳吧

1、完成一個上傳表單upload.jsp和下載頁面down.jsp

<form action="upload_upload" method="post" enctype="multipart/form-data">  <input type="file" name="files"><br>  <input type="file" name="files"><br>  <input type="submit" value="上傳"></form>
<s:iterator value="filesFileName" id="file">  <a href='down_down?filename=<s:property value="#file"/>'><s:property value="#file"/></a><br></s:iterator>

2、上傳和下載Action

public class UploadAction extends ActionSupport{private File[] files;private String[] filesFileName;public File[] getFiles(){return files;}public void setFiles(File[] files){this.files = files;}public String[] getFilesFileName(){return filesFileName;}public void setFilesFileName(String[] filesFileName){this.filesFileName = filesFileName;}public String[] getFilesContentType(){return filesContentType;}public void setFilesContentType(String[] filesContentType){this.filesContentType = filesContentType;}private String[] filesContentType;public String upload(){System.out.println("upload方法執行了...............");String path=ServletActionContext.getServletContext().getRealPath("upload");System.out.println("paht==>"+path);FileInputStream is=null;FileOutputStream os=null;for(int i=0;i<files.length;i++){try{is=new FileInputStream(files[i]);os=new FileOutputStream(new File(path,filesFileName[i]));int len=-1;byte[]buffer=new byte[1024];while(-1!=(len=is.read(buffer))){os.write(buffer, 0, len);}is.close();os.close();} catch (FileNotFoundException e){// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e){// TODO Auto-generated catch blocke.printStackTrace();}}ServletActionContext.getRequest().getSession().setAttribute("filename", filesFileName);return "success";}}

 

public class DownAction extends ActionSupport{private String filename;public String getFilename(){return filename;}public void setFilename(String filename){try{this.filename = new String(filename.getBytes("iso-8859-1"),"utf-8");} catch (UnsupportedEncodingException e){// TODO Auto-generated catch blocke.printStackTrace();}}public InputStream getDownFile(){return ServletActionContext.getServletContext().getResourceAsStream("/upload/"+filename);}public String down(){return "success";}}

 

3、設定檔struts.xml

<action name="upload_*" class="com.action.UploadAction" method="{1}"> <result name="success">/result.jsp</result> <result name="input">/upload.jsp</result> </action>   <action name="down_*" class="com.action.DownAction" method="{1}"> <result type="stream"> <param name="inputName">downFile</param> <param name="contentDisposition">attachment;filename="${filename}"</param> </result> </action>

 

聯繫我們

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