• 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>
  • 相关阅读:
    ASP.NET实现文件下载的功能
    优先队列的使用
    关于CSS设置样式的一点建议
    ASP.NET后台弹出消息的方法
    创建漂亮的 CSS 按钮的 10 个代码片段(转载)
    2020高考游记
    最后的OI(HAOI2020游记)
    2020 2 2 20 20
    祭最后的亡魂
    一个感人至深的故事
  • 原文地址:https://www.cnblogs.com/meetcomet/p/3413211.html
Copyright © 2020-2023  润新知