struts2完成图片上传

图片上传

在这里插入图片描述

文件上传:

三种上传方案
1、上传到tomcat服务器
2、上传到指定文件目录,添加服务器与真实目录的映射关系,从而解耦上传文件与tomcat的关系
3、在数据库表中建立二进制字段,将图片存储到数据库

upload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="${pageContext.request.contextPath}/sy/clz_upload.action" method="post" enctype="multipart/form-data">
	<input type="hidden" name="cid" value="${clz.cid }"></br>
	<input type="hidden" name="cname" value="${clz.cname }"></br>
	<input type="hidden" name="cteacher" value="${clz.cteacher }"></br>
	<!-- 注意:name对应的值决定了,子控制器action属性的命名 -->
	<input type="file" name="file">
	<input type="submit">
	</form>
</body>
</html>

struts-sy.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
	"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<!-- 细分action 让他分类 -->
<!-- 
	/user/bookAction?methodName=list
	/sy/bookAction?methodName=list
 -->
	<package name="sy" extends="base" namespace="/sy">
		<interceptors>
			<interceptor name="one" class="com.xyx.crud.interceptor.OneInterceptor"></interceptor>
			<interceptor name="two" class="com.xyx.crud.interceptor.TwoInterceptor"></interceptor>
		</interceptors>
		<action name="/demo_*" class="com.xyx.web.HelloAction" method="{1}">
			<result name="rs">/rs.jsp</result>
		</action>
		
		<action name="/stack_*" class="com.xyx.test.DemoAction" method="{1}">
			<result name="rs">/rs.jsp</result>
		</action>
		
		<action name="/clz_*" class="com.xyx.crud.web.ClazzAction" method="{1}">
			<!-- <interceptor-ref name="one"></interceptor-ref>
			<interceptor-ref name="two"></interceptor-ref> -->
			<result name="list">/clzList.jsp</result>
			<result name="preSave">/clzEdit.jsp</result>
			<result name="toList" type="redirectAction">clz_list</result>
			<result name="toUpload">/upload.jsp</result>	
		
		</action>
		
		
	
	</package>
</struts>

ClazzAction

ClazzAction控制器,我们先是用struts的完成了图片上传,然后再自己写了一个缓冲流的文件上传,提高了上传的效率

package com.xyx.crud.web;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ModelDriven;
import com.xyx.crud.dao.ClazzDao;
import com.xyx.crud.entity.Clazz;
import com.xyx.crud.util.PageBean;

public class ClazzAction extends BaseAction implements ModelDriven<Clazz>{
	private ClazzDao clzDao=new ClazzDao();
	private Clazz clz=new Clazz();//接受前台传过来的参数
	//img/imgContentType/imgFileName
	private File file;
	private String fileContentType;
	private String fileFileName;

	
	
	public String list() {
		PageBean pageBean =new PageBean();
		pageBean.setRequest(request);//pageBean 初始化
		try {
			List<Clazz> list = this.clzDao.list(clz, pageBean);
			//this.result=this.clzDao.list(clz, pageBean);
			request.setAttribute("clzList", list);//查询
			request.setAttribute("pageBean", pageBean);//分页
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "list";
	}

	/**
	 * 跳转新增页面的公用修改方法
	 * @return
	 */
	public String preSave() {
		if(clz.getCid()!=0) {
			Clazz c;
			try {
				c = this.clzDao.list(clz, null).get(0);//查询数据
				request.setAttribute("clz", c);//把数据传到页面上去
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return "preSave";
	}
	
	/**
	 * 新增
	 * @return
	 */
	public String add() {
			try {
				result=this.clzDao.add(clz);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "toList";
		}
	
	/**
	 * 直接上传图片
	 * @return
	 */
	public String upload() {
		try {
			//注意:在Linux下是没有E盘的,Linux下只有一个盘符,那么意味着,当打包到Linux服务器的
			//时候需要改动代码,这个时候通常是怎么解决的   1:将targetPath对应的目录,配置到资源文件,通过Propperties
			//类经行动态读取,那么需要将项目发布到Linux的时候,只需要改变xxx.properties文件中targetPath=/xxx/001/img
			//实际图片存储的位置
			String targetDir="C:/temp/T224"; 
			//存储到数据库中的地址
			String severPath="/upload";
//			FileUtils.copyFile(file, new File(targetDir+"/"+fileFileName));//源目的地,指定目的地
			copyBufFile(file, new File(targetDir+"/"+fileFileName));
			//注意:数据库存放是网络请求地址,而不是本地图片存放地址
			//存放真实的路径
			clz.setPic(severPath+"/"+fileFileName);
			this.clzDao.edit(clz);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toList";
	}
	
	/**
	 * FileUtils.copyFile的底层,并且通过缓冲区经行增强
	 * @param source
	 * @param target
	 * @throws Exception 
	 */
	public void copyBufFile(File source,File target) throws Exception {
		BufferedInputStream in=new BufferedInputStream(new FileInputStream(source));
		BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(target));
		byte[] bbuf=new byte[1024];
		int len=0;
		while((len=in.read(bbuf))!=-1) {
			out.write(bbuf,0,len);
		}
		in.close();
		out.close();
	}
	
	
	/**
	 * 跳转文件上传页面
	 * @return
	 */
	public String preUpload() {
		Clazz c;
		try {
			c= this.clzDao.list(clz, null).get(0);//查询数据
			request.setAttribute("clz", c);//把数据传到页面上去
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "toUpload";
	}
	
	
	
	
	/**
	 * 修改
	 * @return
	 */
	public String edit() {
		try {
			this.clzDao.edit(clz);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "toList";
	}
	
	/**
	 * 删除
	 * @return
	 */
	public String del() {
			try {
				this.clzDao.del(clz);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		return "toList";
	}

	@Override
	public Clazz getModel() {
		return clz;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String fileFileName) {
		this.fileFileName = fileFileName;
	}

}

Server.xml

在中间加上下面这行代码,不然会找不到图片路径
<Context docBase="C:/temp/T224/" path="/T224_struts/upload"/>

在这里插入图片描述

拦截器Interceptor

拦截的两种方法

implements Interceptor
extends AbstractInterceptor

拦截器的原理:

大部分时候,拦截器方法都是通过代理的方式来调用的。Struts 2的拦截器实现相对简单。当请求到达Struts 2的ServletDispatcher时,Struts 2会查找配置文件,并根据其配置实例化相对的拦截器对象,然后串成一个列表(list),最后一个一个地调用列表中的拦截器。Struts2拦截器是可插拔的,拦截器是AOP的一种实现。Struts2拦截器栈就是将拦截器按一定的顺序联结成一条链。在访问被拦截的方法或字段时,Struts2拦截器链中的拦截器就会按其之前定义的顺序被调用。

如何定义拦截器:

自定义一个拦截器需要三步:
1 .自定义一个实现Interceptor接口(或者继承自AbstractInterceptor)的类。
2 .在struts.xml中注册上一步中定义的拦截器。
3 .在需要使用的Action中引用上述定义的拦截器,为了方便也可将拦截器定义为默认的拦截器,这样在不加特殊声明的情况下所有的Action都被这个拦截器拦截

与过滤器的区别

过滤器可以简单理解为“取你所想取”,忽视掉那些你不想要的东西;拦截器可以简单理解为“拒你所想拒”,关心你想要拒绝掉哪些东西,比如一个BBS论坛上拦截掉敏感词汇。
1.拦截器是基于java反射机制的,而过滤器是基于函数回调的。
2.过滤器依赖于servlet容器,而拦截器不依赖于servlet容器。
3.拦截器只对action起作用,而过滤器几乎可以对所有请求起作用。
4.拦截器可以访问action上下文、值栈里的对象,而过滤器不能。
5.在action的生命周期里,拦截器可以多起调用,而过滤器只能在容器初始化时调用一次。
参考资料

在这里插入图片描述

在这里插入图片描述

struts-sy.xml

<interceptors>
			<interceptor name="one" class="com.xyx.crud.interceptor.OneInterceptor"></interceptor>
			<interceptor name="two" class="com.xyx.crud.interceptor.TwoInterceptor"></interceptor>
		</interceptors>

<interceptor-ref name="one"></interceptor-ref>
<interceptor-ref name="two"></interceptor-ref>

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值