用数组很简单。struts2的类型转换还是很强啊。

JSP:

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

Action:和一个文件的很相似

public class FileUploadByArrayAction extends ActionSupport {

     private File[] doc;
     private String[] docContentType; //文件类型属性
     private String[] docFileName; //文件名
   
  


    public String[] getDocFileName() {
        return docFileName;
    }


    public void setDocFileName(String[] docFileName) {
        this.docFileName = docFileName;
    }


    private String path;


     public String execute() {

         for (int i=0;i<doc.length;i++)
         {
           String docfilename = getFileName(getDocFileName()[i]);
           FileOutputStream fos = null;
           FileInputStream fis = null;
          try {
              fos = new FileOutputStream(getPath() + "\\" + docfilename);
              fis = new FileInputStream(getDoc()[i]);
              byte[] buffer = new byte[1024];
              int len = 0;
              while ((len = fis.read(buffer)) > 0) {
                  fos.write(buffer, 0, len);
              }
          } catch (Exception e) {
             System.out.println("文件上传失败");
             e.printStackTrace();
          } finally {
             close(fos, fis);
          }
         }
         

         return SUCCESS;
        
    }
        

private void close(FileOutputStream fos, FileInputStream fis) {
    if (fis != null) {
        try {
            fis.close();
        } catch (IOException e) {
            System.out.println("FileInputStream关闭失败");
            e.printStackTrace();
        }
    }
    if (fos != null) {
        try {
            fos.close();
        } catch (IOException e) {
            System.out.println("FileOutputStream关闭失败");
            e.printStackTrace();
        }
    }
}

     //生成一个新的文件名
    private String getFileName(String fileName) {
        int position = fileName.lastIndexOf(".");
        String extension = fileName.substring(position);
        return System.currentTimeMillis()+extension;
    }


    public String getPath() {
        return ServletActionContext.getServletContext().getRealPath(path);
    }


    public void setPath(String path) {
        this.path = path;
    }


    public File[] getDoc() {
        return doc;
    }


    public void setDoc(File[] doc) {
        this.doc = doc;
    }


    public String[] getDocContentType() {
        return docContentType;
    }


    public void setDocContentType(String[] docContentType) {
        this.docContentType = docContentType;
    }


    



}
View Code

struts.xml

        <action name="doUploadByArray" class="com.meetcomet.util.FileUploadByArrayAction">
            <param name="path">/upload</param>
            <result name="input">/index.jsp</result>
            <result name="success">/upload_success.jsp</result>
        </action>

 

 

posted on 2013-11-07 20:59  meetcomet  阅读(205)  评论(0编辑  收藏  举报