Struts 多个文件,拦截器,文件上传

Struts 引入多个文件 

<include file="struts_user.xml"></include>
<include file="struts_login.xml"></include>

重定向 : 默认转发:

<result name="add_submit" type="redirect">/user_list.do</result>


	<!-- 默认action  action找不到时 执行index.jsp页面-->
		<default-action-ref name="index"/>
		<action name="index">
			<result>/index.jsp</result>
		</action>


拦截器

  <interceptors>
            <!-- 定义拦截器 
                name:拦截器名称
                class:拦截器类路径
             -->
            <interceptor name="timer" class="com.kay.timer"></interceptor>
            <interceptor name="logger" class="com.kay.logger"></interceptor>
            <!-- 定义拦截器栈 -->
            <interceptor-stack name="mystack">
                <interceptor-ref name="timer"></interceptor-ref>
                <interceptor-ref name="logger"></interceptor-ref>
            </interceptor-stack>
        </interceptors>
        
        <!-- 定义默认的拦截器 每个Action都会自动引用
         如果Action中引用了其它的拦截器 默认的拦截器将无效 -->
        <default-interceptor-ref name="mystack"></default-interceptor-ref>

创建自定义拦截器
public class MyInterceptor extends AbstractInterceptor {

   public String intercept(ActionInvocation invocation)throws Exception{

      /* let us do some pre-processing */
      String output = "Pre-Processing"; 
      System.out.println(output);

      /* let us call action or next interceptor */
      String result = invocation.invoke();

      /* let us do some post-processing */
      output = "Post-Processing"; 
      System.out.println(output);

      return result;
   }
}




文件上传:

jsp 页面:
<form action="user_file" method="post" enctype="multipart/form-data">
		 姓名:<input type="text" name="username"/><br>
		 密码:<input type="text" name="password"/><br>
		 <input type="file" name="myfile" ><br>
		 <input type="submit" value="提交"/><br>
	</form> 

配置文件上传的最大值

struts.multipart.maxSize=1000000


action :
 需要set方法

	private File myfile; //上传文件  name值  
	private String myfileFileName;	//文件名   name值+FileName
    private String myfileContentType; //文件类型   name值+ContentType
	public String file() throws IOException {
		System.out.println("myfileFileName:"+myfileFileName+" name:"+username+" "+myfile);
        
		String realpath = ServletActionContext.getServletContext().getRealPath("/images");
        if(myfile!=null){ 
        File savefile = new File(new File(realpath), myfileFileName);
          if (!savefile.getParentFile().exists())
              savefile.getParentFile().mkdirs();
          FileUtils.copyFile(myfile, savefile);
		
          System.out.println(savefile);
          }
		return "file";
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值