• 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>
  • 相关阅读:
    get和post区别
    linux查看是否被入侵
    (转)MSSQL 各个发行版本版本号以及Compact 版本号
    (转)玩转-数据库行列转换
    (转)理解SQL SERVER中的分区表
    (转)使用CruiseControl+SVN+ANT实现持续集成之三
    (转)使用CruiseControl+SVN+ANT实现持续集成之二
    (转)使用SVN+CruiseControl+ANT实现持续集成之一
    (转)持续化集成工具CruiseControl.NET
    (转)DockPanel 右键增加关闭,除此之外全部关闭的功能
  • 原文地址:https://www.cnblogs.com/meetcomet/p/3413211.html
Copyright © 2020-2023  润新知