struts2文件上传类型的过滤

第一种解决方案:

1.手动实现文件过滤:

判断上传的文件是否在允许的范围内
定义该Action允许上传的文件类型 private String allowTypes;
利用Struts2的输入效验判断用户的输入的文件是否合理

UploadAction.java

package 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 {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String title;
	private File uploadfile;
	private String uploadfileContentType;
	private String uploadfileFileName;
	private String savePath;
	private String allowType;//定义该Action允许上传的文件类型
	
	public boolean check(String type){
    	String[] types=allowType.split(",");
    	for(String s:types){
    		if(s.equals(type)){
    			return true;
    		}
    	}
		return false;
    	
    }
    public void  validate(){
    	boolean b=check(uploadfileContentType);
    	if(!b){
    		addFieldError("upload","上传文件错误");
    	}
    }
	public String getAllowType() {
		return allowType;
	}

	public void setAllowType(String allowType) {
		this.allowType = allowType;
	}

	public String upload()  throws  Exception {
		
		
		FileInputStream fis = new FileInputStream(getUploadfile());
        FileOutputStream  fos=new FileOutputStream(getSavePath()+"\\"+getUploadfileFileName());
        byte[] buffer=new byte[1024];
        int len=0;
        while((len=fis.read(buffer))>0){
        	fos.write(buffer,0,len);
        }
        fos.close();
        fis.close();
        
		return SUCCESS;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}


	public File getUploadfile() {
		return uploadfile;
	}

	public void setUploadfile(File uploadfile) {
		this.uploadfile = uploadfile;
	}

	public String getUploadfileContentType() {
		return uploadfileContentType;
	}

	public void setUploadfileContentType(String uploadfileContentType) {
		this.uploadfileContentType = uploadfileContentType;
	}

	public String getUploadfileFileName() {
		return uploadfileFileName;
	}

	public void setUploadfileFileName(String uploadfileFileName) {
		this.uploadfileFileName = uploadfileFileName;
	}

	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath(savePath);//得到绝对路径
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

}

struts.xml





	
		
			
		        image/x-png,file/txt,image/jpeg
			
			/uploadFiles
			
				/success.jsp
			
			
				/index.jsp
			
		
	


index.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 'index.jsp' starting page
	
	
	    
	
	
	
  
  
  
  
    
文件名: 文件:

success.jsp

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

  
  
  <%
String path = request.getContextPath();
  System.out.println(path);
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
System.out.println(basePath);
%>
  
  上传成功!
文件标题:
文件为:"/>

第二种解决方案:

2.拦截器实现文件过滤:

配置fileUpload拦截器两个参数:
allowedTypes:允许上传文件的类型,多个值用,分开
maximumSize:允许上传文件的大小,单位字节。

UploadAction.java

package 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 {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String title;
	private File uploadfile;
	private String uploadfileContentType;
	private String uploadfileFileName;
	private String savePath;
	private String allowedTypes;
	
	
        public String getAllowedTypes() {
		return allowedTypes;
	}
	public void setAllowedTypes(String allowedTypes) {
		this.allowedTypes = allowedTypes;
	}

	public String upload()  throws  Exception {
		
		
		FileInputStream fis = new FileInputStream(getUploadfile());
        FileOutputStream  fos=new FileOutputStream(getSavePath()+"\\"+getUploadfileFileName());
        byte[] buffer=new byte[1024];
        int len=0;
        while((len=fis.read(buffer))>0){
        	fos.write(buffer,0,len);
        }
        fos.close();
        fis.close();
        
		return SUCCESS;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}


	public File getUploadfile() {
		return uploadfile;
	}

	public void setUploadfile(File uploadfile) {
		this.uploadfile = uploadfile;
	}

	public String getUploadfileContentType() {
		return uploadfileContentType;
	}

	public void setUploadfileContentType(String uploadfileContentType) {
		this.uploadfileContentType = uploadfileContentType;
	}

	public String getUploadfileFileName() {
		return uploadfileFileName;
	}

	public void setUploadfileFileName(String uploadfileFileName) {
		this.uploadfileFileName = uploadfileFileName;
	}

	public String getSavePath() {
		return ServletActionContext.getServletContext().getRealPath(savePath);//得到绝对路径
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

}

struts.xml





	
		
			
			 
			   image/x-png,image/gif,image/jpeg
			   20000000
			
			 
			/uploadFiles
			
				/success.jsp
			
			
				/index.jsp