Struts2多檔案上傳

來源:互聯網
上載者:User

標籤:

Struts2多檔案上傳

 1 package com.tcf.action; 2  3 import java.io.File; 4 import java.io.FileInputStream; 5 import java.io.FileNotFoundException; 6 import java.io.FileOutputStream; 7 import java.io.IOException; 8  9 import com.opensymphony.xwork2.ActionSupport;10 11 public class UploadesAction extends ActionSupport {12     private File[] file;13     private String[] fileContentType;14     private String[] fileFileName;15     private String savePath;16     public File[] getFile() {17         return file;18     }19 20     public void setFile(File[] file) {21         this.file = file;22     }23 24     public String[] getFileContentType() {25         return fileContentType;26     }27 28     public void setFileContentType(String[] fileContentType) {29         this.fileContentType = fileContentType;30     }31 32     public String[] getFileFileName() {33         return fileFileName;34     }35 36     public void setFileFileName(String[] fileFileName) {37         this.fileFileName = fileFileName;38     }39 40     public String getSavePath() {41         return savePath;42     }43 44     public void setSavePath(String savePath) {45         this.savePath = savePath;46     }47 48     public String upload(){49         byte[] buf = new byte[1024];50         try {51             for(int i = 0;i<file.length;i++){52                 FileInputStream fis = new FileInputStream(file[i]);//源53                 FileOutputStream fos = new FileOutputStream(savePath+"/"+fileFileName[i]);//放到哪裡54                 int len = 0;55                 while((len=fis.read(buf))!=-1){56                     fos.write(buf,0,len);57                 }58                 //重新整理緩衝59                 //fos.flush();60                 fos.close();61                 fis.close();62             }63         } catch (FileNotFoundException e) {64             // TODO Auto-generated catch block65             e.printStackTrace();66         } catch (IOException e) {67             // TODO Auto-generated catch block68             e.printStackTrace();69         }70         return SUCCESS;71     }72 }

struts.xml

1 <action name="uploades" class="com.tcf.action.UploadesAction" method="upload">2     <param name="savePath">E:/temp</param>3     <result>uploadSuccess.jsp</result>4 </action>

upload.jsp

1 <s:form action="uploades.action" enctype="multipart/form-data" method="post">2         <s:file name="file" />3         <s:file name="file" />4         <s:file name="file" />5         <s:submit></s:submit>6 </s:form>

 

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.