zoukankan      html  css  js  c++  java
  • 多个文件上传

    //准备文件

    1.上传多个文件页面

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <%@taglib uri="/struts-tags" prefix="s" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>登录页面</title>
    
      </head>
      
      <body>
       <s:form action="upload.action" enctype="multipart/form-data" method="post">
            <s:file name="upload" label="选择文件" />
    		<br />
    		<s:file name="upload" label="选择文件" />
    		<br />
    		<s:file name="upload" label="选择文件" />
    		<br />
    		<s:submit name="submit" value="上传文件"></s:submit>
       </s:form>
      </body>
    </html>
    

      2.在struts.xml中配置相应的action

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC 
    	"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    	"http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
    	<!-- 动态方法调用 -->
     
        <constant name="struts.devMode" value="false" />
        
        
        <package name="default" namespace="/" extends="struts-default">
        	
            <action name="upload" class="cn.happy.action.UploadAction" method="execute">
            	<param name="savePath">/image</param>
               <result name="success">/upload/success.jsp</result>
            </action>
        </package>
    </struts>
    

     3.在根据action节点找对应的类

    package cn.happy.action;
    
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    
    import org.apache.struts2.ServletActionContext;
    
    import com.opensymphony.xwork2.ActionSupport;
    
    public class UploadAction extends ActionSupport {
    	/***
    	 * 多文件上传
    	 * @return
    	 */
    	// 上传文件的属性
    		private File[] upload;
    		// 上传文件的类型
    		private String[] uploadContentType;
    		// 上传文件的名称
    		private String[] uploadFileName;
    		// 上传文件的地址
    		private String savePath;
    		
    		@Override	
    		public String execute() throws Exception{
    			byte[] buffer=new byte[1024];
    			for(int i=0;i<upload.length;i++){
    				//建立上传文件的出入流
    				FileInputStream fis=new FileInputStream(getUpload()[i]);
    				//建立上传文件的输出流, getImageFileName()[i]
    				FileOutputStream fos=new FileOutputStream(getSavePath()+"\"+getUploadFileName()[i]);
    			    int length=fis.read(buffer);
    			    while(length>0){
    			    	fos.write(buffer,0,length);
    			    	length=fis.read(buffer);
    			    }
    			    fis.close();
    				fos.flush();
    				fos.close();
    			}
    			
    			return SUCCESS;
    		}
    	
    		public File[] getUpload() {
    			return upload;
    		}
    		public void setUpload(File[] upload) {
    			this.upload = upload;
    		}
    		
    		public String[] getUploadContentType() {
    			return uploadContentType;
    		}
    		public void setUploadContentType(String[] uploadContentType) {
    			this.uploadContentType = uploadContentType;
    		}
    		
    		
    		public String[] getUploadFileName() {
    			return uploadFileName;
    		}
    		public void setUploadFileName(String[] uploadFileName) {
    			this.uploadFileName = uploadFileName;
    		}
    		
    		public String getSavePath() {
    			return ServletActionContext.getServletContext().getRealPath(savePath);
    		}
    		public void setSavePath(String savePath) {
    			this.savePath = savePath;
    		}
    }
    

    4.如果成功就去找成功页面

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <%@taglib uri="/struts-tags" prefix="s" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>成功页面</title>
      </head>
      
      <body>
           您所上传的文件是:<s:property value="uploadFileName"/><br/>
    	文件类型:<s:property value="uploadContentType"/>
      </body>
    </html>
    

      

  • 相关阅读:
    写了这么久前端,你知道浏览器原理吗?
    史上最全的web前端开发程序员学习清单!
    常见前端面试题及答案
    “下辈子再也不当程序员了,我被黑够了”
    什么是web前端,全栈工程师就业前景怎么样?
    要嫁就嫁程序员,钱多话少死得早!
    想转行做web前端工程师,必学这6大技能
    测试用例设计总结
    python读取一个英文文件,并记录每个单词出现的次数,降序输出
    python读取一个文件的每一行判断是否为素数,并把结果写到另一个文件中
  • 原文地址:https://www.cnblogs.com/yangronglin/p/5945187.html
Copyright © 2011-2022 走看看