分享struts2的文件拦截器使用

   ,前面几个项目中阿堂是一直用的struts1.2,最近打算在一些新项目中做struts2..在看struts2相关内容时,发现在struts2中对于上传文件的过滤器的设置竟是如此之简单?过滤文件类型和文件大小,只需在struts.xml文件配置一个默认的defautlStack拦截器就可以轻松实现了。。这里,还是把它记下来,作为以后项目中用时的参考吧
       struts2提供了一个文件上传的拦截器,通过配置该拦截器可以轻松地实现文件过滤。struts2文件上传的过滤器是fileUpload,为了该拦截器起作用,只需要在该Action中配置该拦截器引用即可


配置fileUpload拦截器时,可以为其指定两个参数
allowedType:该参数指定允许上传的文件类型,多个文件类型之间以英文逗号(,)隔开
maximumSize:该参数指定该允许文件上传的文件大小,单位是字节

       通过配置fileUpload拦截器,可以更轻松地实现文件过滤,当文件失败后,系统自动转入input逻辑视图,因此必须为该Action配置名为input的逻辑视图。除此之外,还需显式地为该Action配置defaultStack的拦截器引用

下面,我通过一个demo简单测试如下
通过拦截器来实现文件的过滤的配置文件struts.xml如下


<?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.custom.i18n.resources" value="globalMessages"/> 
 <package name="upload" extends="struts-default"> 
  <action name="upload" class="lee.UploadAction">
            <interceptor-ref name="fileUpload">
                <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg</param>
                <param name="maximumSize">2000</param>
            </interceptor-ref>
            <interceptor-ref name="defaultStack"/>           

            <param name="savePath">/upload</param>
   <result name="input"> /upload.jsp</result>
   <result>/succ.jsp</result> 
  </action>
  
 </package>
</struts> 


对应的Action如下

package com.ish.strutsc.action;

import com.opensymphony.xwork2.Action;
import org.apache.struts2.ServletActionContext;
import java.io.File;
import java.io.*;

import com.opensymphony.xwork2.ActionSupport;
\public class UploadAction extends ActionSupport
{
 private String title;
    private File upload;
    private String uploadContentType;
    private String uploadFileName;

 //接受依赖注入的属性
    private String savePath;
 //接受依赖注入的方法
    public void setSavePath(String value)
 {
        this.savePath = value;
    }

    private String getSavePath() throws Exception
 {
        return ServletActionContext.getRequest().getRealPath(savePath);
    }
 
 public void setTitle(String title) {
  this.title = title;
 }

 public void setUpload(File upload) {
  this.upload = upload;
 }

 public void setUploadContentType(String uploadContentType) {
  this.uploadContentType = uploadContentType;
 }

 public void setUploadFileName(String uploadFileName) {
  this.uploadFileName = uploadFileName;
 }

 public String getTitle() {
  return (this.title);
 }

 public File getUpload() {
  return (this.upload);
 }

 public String getUploadContentType() {
  return (this.uploadContentType);
 }

 public String getUploadFileName() {
  return (this.uploadFileName);
 }
 @Override
    public String execute() throws Exception
 {
  System.out.println("开始上传单个文件-----------------------");
  System.out.println(getSavePath());
  System.out.println("==========" + getUploadFileName());
  System.out.println("==========" + getUploadContentType());
  System.out.println("==========" + getUpload());
  //以服务器的文件保存地址和原文件名建立上传文件输出流
  FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName());
  FileInputStream fis = new FileInputStream(getUpload());
  byte[] buffer = new byte[1024];
  int len = 0;
  while ((len = fis.read(buffer)) > 0)
  {
   fos.write(buffer , 0 , len);
  }
        return SUCCESS;
    }
}


web.xml文件的配置如下
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4"
 xmlns="http://java.sun.com/xml/ns/j2ee"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <filter>
        <filter-name>struts</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <filter>
        <filter-name> struts-cleanup </filter-name>
        <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts-cleanup</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


</web-app>

 

 

globalMessages.properties文件如下

struts.messages.error.content.type.not.allowed=您上传的文件类型只能是图片文件!请重新选择!
struts.messages.error.file.too.large=您要上传的文件太大,请重新选择!
struts.messages.error.uploading=上传文件时出现了未知的错务,请与阿堂联系

 

分享struts2的文件拦截器使用

分享struts2的文件拦截器使用 

你可能感兴趣的:(struts2,职场,休闲)