• 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>
  • 相关阅读:
    紧急情况下测试周期被压缩该如何测试?
    测试人员提高业务掌握度的方案
    Android客户端性能测试(一):使用APT测试Android应用性能
    转载:员工价值——如何体现自己价值,如何被自己的领导认可
    如何提高测试用例复用性和完善测试用例
    如何理解栈(栈的实现方式)
    Android 自动化测试—robotium(七) 使用Junit_report测试报告
    Android 自动化测试—robotium(八) 拖拽
    解决ADT升级报错
    敏捷测试的关键理念
  • 原文地址:https://www.cnblogs.com/meetcomet/p/3413211.html
Copyright © 2020-2023  润新知