zoukankan      html  css  js  c++  java
  • Struts 2读书笔记使用Struts 2控制多文件上传

              在前面我已经介绍过了利用Struts 2控制单文件上传:Struts 2读书笔记-----使用Struts 2控制文件上传 .其实对于多文件上传也差不多。加入我们要同时控制三个文件进行上传。那么页面得有三个文件上传域。在这里主要介绍采用数字和list来控制文件上传。

     

             一、利用数组

              利用数组来封装3个文件域。为了让数组一次性封装三个文件,我们需要将三个文件域的name属性设置为一样的。如下:

        <form action="upload.action" method="POST" enctype="multipart/form-data">
        	文件标题:<input type="text" name="title"/><Br />
        	第一个文件:<input type="file" name="upload"/><br/>
        	第二个文件:<input type="file" name="upload"/><br/>
        	第三个文件:<input type="file" name="upload"/><br/>
        	<input type="submit" value="上传"/>
        </form>


            在处理上传的过程中,我们需要3个数组来分别封装文件名、文件类型、文件内容

         

    public class UploadToArrayAction extends ActionSupport {
    	private String title;                          // 文件标题
    	private File[] upload;                         // 使用file数组封装多个文件域对应的文件内容
    	private String[] uploadContentType;             // 使用字符串数组封装多个文件域对应的文件类型
    	private String[] uploadFileName;               // 使用字符串数组封装对个文件域对应的文件名
    
    	private String savePath;                       // 动态设置文件属性
    
    	public String getTitle() {
    		return title;                        
    	}
    
    	public void setTitle(String title) {
    		this.title = title;
    	}
    
    	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 savePath;
    	}
    
    	public void setSavePath(String savePath) {   
    		this.savePath = savePath;
    	}
    
    	public String execute() throws Exception{
    		//取得需要上传的文件数组
    		File[] files = getUpload();
    		//遍历每个需要上传的文件
    		for (int i = 0; i < files.length; i++) {
    			//以服务器的文件保存地址和源文件名建立上传文件输出流
    			String root = ServletActionContext.getRequest().getSession().getServletContext().getRealPath("/upload");
    			FileOutputStream fos = new FileOutputStream(root+"\\"+getUploadFileName()[i]);
    			//以每个需要上传的文件建立输入流
    			FileInputStream fis = new FileInputStream(files[i]);
    			//将每个需要上传的文件写入
    			byte[] buffer = new byte[1024];
    			int length = 0;
    			while ((length=fis.read(buffer))>0) {
    				fos.write(buffer,0,length);
    			}
    			fos.close();
    		}
    		return SUCCESS;
    	}
    }

       

              struts.xml配置文件的配置

    <package name="mystruts" extends="struts-default">
    		<action name="upload" class="com.app.action.UploadToArrayAction">
    			<!-- 动态设置Action的savePath属性 -->
    			<param name="savePath">/upload</param>
    			<!-- 配置文件上传的拦截器 -->                       
    			<interceptor-ref name="fileUpload">
    				<!-- 配置允许上传的文件类型 -->
    				<param name="allowedTypes">image/png,image/gif,image/jpeg</param>
    				<param name="maximumSize">20480000</param>
    			</interceptor-ref>
    			
    			<!-- 配置系统默认的拦截器 -->
    			<interceptor-ref name="defaultStack"></interceptor-ref>
    			<result name="success">/success.jsp</result>
    			<result name="input">/upload.jsp</result>
    		</action>
    	</package>

              配置这个Action与配置单文件上传的Action其实是没有什么其他的区别的。

             

              二、利用List

               其实利用List和利用数组时没有什么区别的。只是将Array数组改为了泛型了list而已了。

             

    public class UploadToListAction extends ActionSupport {
    	private String title; // 文件标题
    	private List<File> upload; // 使用file数组封装多个文件域对应的文件内容
    	private List<String> uploadContentType; // 使用字符串数组封装多个文件域对应的文件类型
    	private List<String> uploadFileName; // 使用字符串数组封装对个文件域对应的文件名
    
    	private String savePath; // 动态设置文件属性
    
    	public String getTitle() {
    		return title;
    	}
    
    	public void setTitle(String title) {
    		this.title = title;
    	}
    
    	public List<File> getUpload() {
    		return upload;
    	}
    
    	public void setUpload(List<File> upload) {
    		this.upload = upload;
    	}
    
    	public List<String> getUploadContentType() {
    		return uploadContentType;
    	}
    
    	public void setUploadContentType(List<String> uploadContentType) {
    		this.uploadContentType = uploadContentType;
    	}
    
    	public List<String> getUploadFileName() {
    		return uploadFileName;
    	}
    
    	public void setUploadFileName(List<String> uploadFileName) {
    		this.uploadFileName = uploadFileName;
    	}
    
    	public String getSavePath() {
    		return savePath;
    	}
    
    	public void setSavePath(String savePath) {
    		this.savePath = savePath;
    	}
    
    	public String execute() throws Exception {
    		// 取得需要上传的文件数组
    		List<File> files = getUpload();
    		// 遍历每个需要上传的文件
    		for (int i = 0; i < files.size(); i++) {
    			String root = ServletActionContext.getRequest().getSession().getServletContext().getRealPath("/upload");
    			FileOutputStream fos = new FileOutputStream(root+"\\"+getUploadFileName().get(i));
    			FileInputStream fis = new FileInputStream(files.get(i));
    			byte[] buffer = new byte[1024];
    			int length = 0;
    			while((length=fis.read())>0){
    				fos.write(buffer,0,length);
    			}
    			fos.close();
    		}
    		return SUCCESS;
    	}
    }
    


              其他的配置和数组的差不多,就不写了................



     

  • 相关阅读:
    spring定时器的cronexpression表达式
    Mybatis Generator的model生成中文注释,支持oracle和mysql(通过实现CommentGenerator接口的方法来实现)
    ORA-12505, TNS:listener does not currently know of SID given in connect desc
    The Network Adapter could not establish the connection
    Shell中的>/dev/null 2>&1 与 2>&1 >/dev/null 与&>/dev/null 的区别
    大道至简、大智若愚—GO语言最佳详解实践
    rsync使用详解
    一次TIME_WAIT和CLOSE_WAIT故障和解决办法
    Go的CSP并发模型实现:M, P, G
    如何优雅打印nginx header和body
  • 原文地址:https://www.cnblogs.com/oversea201405/p/3752273.html
Copyright © 2011-2022 走看看