zoukankan      html  css  js  c++  java
  • Struts2学习8--文件上传(多个文件上传)

    用数组很简单。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>
  • 相关阅读:
    【BZOJ5286】[HNOI2018]转盘(线段树)
    【BZOJ2003】[HNOI2010]矩阵(搜索)
    【BZOJ2000】[HNOI2000]取石头游戏(贪心,博弈论)
    【BZOJ1998】[HNOI2010]物品调度(并查集,模拟)
    【BZOJ2001】[HNOI2010]城市建设(CDQ分治,线段树分治)
    【BZOJ1925】[SDOI2010]地精部落(动态规划)
    【BZOJ1856】[SCOI2010]字符串(组合数学)
    【BZOJ1826】[JSOI2010]缓存交换(贪心)
    【BZOJ1823】[JSOI2010]满汉全席(2-sat)
    【BZOJ1822】[JSOI2010]冷冻波(二分,网络流)
  • 原文地址:https://www.cnblogs.com/meetcomet/p/3413211.html
Copyright © 2011-2022 走看看