Struts2之文件上传(单文件/多文件)

<一>简述:

Struts2的文件上传其实也是通过拦截器来实现的,只是该拦截器定义为默认拦截器了,所以不用自己去手工配置,


<二>指定用户上传文件的大小,有两种方式

1)默认是在default.properties 文件的 struts.multipart.maxSize=2097152  键值指定为2097152 也就是2M,通过计算 2097152/(1024*1024) = 2 M

那我们可以改变其默认值,只要在src目录下,新建一个 struts.properties 文件,指定上传大小 如下:


一次上传只可以上传10M,不管一次上传多少个文件,按总和计算


2)在struts.xml文件中指定,如图:


其实name就对应struts.properties的键,value对应 值


注意:如果即在struts.properties设定文件上传大小,又在struts.xml 设定文件上传大小,则struts.properties的优先级高于struts.xml,一般在一处指定上传大小即可,推荐 struts.properties



<三>Struts2之单文件上传


1.fileupload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'fileupload.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
     
     
用户名:
上传文件:

2.具体处理上传的 FileUpload.java

package com.struts2.fileupload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 单个文件上传
 * @author Administrator
 * 上传文件其实是上传了两份,
 * 
 * 首先将上传的文件保存到 default.properties 文件中 struts.multipart.saveDir键指定的目录中
 * 默认是空的
 * 保存在  Tomcat 6.0\work\Catalina\localhost\struts2目录下以.tmp后缀名的文件
 * 
 * 如果要在 struts.multipart.saveDir 指定目录, 则可以在 src文件夹下 建一个 struts.properties,
 * 覆盖  default.properties 的某些键值
 * 
 * 还有一份是 存放在自己设定的目录下
 */
public class FileUpload extends ActionSupport {
	
	private String usename ;
	private File file1 ; //具体上传文件的 引用 , 指向临时目录中的临时文件
	private String file1FileName ;  // 上传文件的名字 ,FileName 固定的写法
	private String file1ContentType ; //上传文件的类型, ContentType 固定的写法
	
	public String getUsename() {
		return usename;
	}
	public void setUsename(String usename) {
		this.usename = usename;
	}
	public File getFile1() {
		return file1;
	}
	public void setFile1(File file1) {
		this.file1 = file1;
	}
	public String getFile1FileName() {
		return file1FileName;
	}
	public void setFile1FileName(String file1FileName) {
		this.file1FileName = file1FileName;
	}
	public String getFile1ContentType() {
		return file1ContentType;
	}
	public void setFile1ContentType(String file1ContentType) {
		this.file1ContentType = file1ContentType;
	}
	
	@Override
	public String execute() throws Exception {
		//获取文件存储路径
		String path = ServletActionContext.getRequest().getRealPath("/upload");
		//输出流
		OutputStream os = new FileOutputStream(new File(path,file1FileName));
		//输入流
		InputStream is = new FileInputStream(file1);
		
		byte[] buf = new byte[1024];
		int length = 0 ;
		
		while(-1 != (length = is.read(buf) ) )
		{
			os.write(buf, 0, length) ;
		}
		is.close();
		os.close();
		
		return SUCCESS;
	}
	
	
	

}

3.最终显示结果的页面,filedemo.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'filedemo.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
    上传成功: 
usename:
file:
contentType:


<四>Struts2之多文件上传

1.fileupload.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'fileupload.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
     
     
用户名:
上传文件:
上传文件:

两个上传文件的name属性值要是一样的,后台方便处理


2.具体处理上传文件的FileUpload2.java

多文件上传用集合的方式

package com.struts2.fileupload;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 多文件上传,用集合的方式
 * @author Administrator
 *
 */

public class FileUpload2 extends ActionSupport {
	
	private String usename ;
	private List file1 ;
	private List file1FileName ;
	private List file1ContentType ;
	
	public String getUsename() {
		return usename;
	}
	public void setUsename(String usename) {
		this.usename = usename;
	}
	public List getFile1() {
		return file1;
	}
	public void setFile1(List file1) {
		this.file1 = file1;
	}
	public List getFile1FileName() {
		return file1FileName;
	}
	public void setFile1FileName(List file1FileName) {
		this.file1FileName = file1FileName;
	}
	public List getFile1ContentType() {
		return file1ContentType;
	}
	public void setFile1ContentType(List file1ContentType) {
		this.file1ContentType = file1ContentType;
	}
	
	@Override
	public String execute() throws Exception {
		
		//获取文件存储路径
		String path = ServletActionContext.getRequest().getRealPath("/upload");
		
		for(int i = 0 ; i < file1.size() ; i++ )
		{
			OutputStream os = new FileOutputStream(new File(path,file1FileName.get(i)));
			
			InputStream is = new FileInputStream(file1.get(i));
			
			byte[] buf = new byte[1024];
			int length = 0 ;
			
			while(-1 != (length = is.read(buf) ) )
			{
				os.write(buf, 0, length) ;
			}
			
			is.close();
			os.close();
			
		}
		
		return SUCCESS;
	}

}

3.用于显示的界面filedemo.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'filedemo2.jsp' starting page
    
	
	
	    
	
	
	

  
  
  
              上传成功:
usename:
文件:

遍历集合的方式,用struts2提供的标签 iterator 可以实现

             
                                                 文件:
 
              

               toUpperCase()字符串的方法是把字母转为大写
             


下载链接:

1)Servlet 文件上传 ————  点击打开链接

2)Struts2之下载  点击打开链接






你可能感兴趣的:(Struts2)