Struts2的图片上传和拦截器

Struts2的图片上传和拦截器

图片上传是在Struts2的增删查改的基础上进行的修改

文件上传

三种方案
1.1:上传到tomcat服务器
1.2:上传到指定文件目录,添加服务器与真实目录的映射关系,从而解耦上传文件与tomcat的关系
文件服务器(常用)
1.3:在数据库表中建立二进制字段,将图片存储到数据库(很少用)

下面我们来实现第二种
案例

Action代码



	//注意如果前台的上传input框的name不一样那么这里也要修改(将三个变量中的file改成你input框的name)
	private File file;
	private String fileContentType;
	private String fileFileName;
	
	//图片上传代码
	
	/**
	 * 上传图片 
	 * @return
	 */
	public String upload() throws Exception{
		//上传到本地的文件夹路径 注意Lunex系统上只有一个盘符,所有这里的路径要进行修改
		String targetDir = "D:/upload";
		//保存到数据库中(通过添加服务器与真实目录的映射关系来访问服务器本地的那张图片)
		String severPath = "/upload";
		//文件上传
		FileUtils.copyFile(file, new File(targetDir + "/" + fileFileName));
		//保存数据库
		clz.setPic(severPath + "/" + fileFileName);
		this.clzDao.edit(clz);
		return "toList";
	}

	/**
	 * 准备上传图片方法
	 * @return
	 */
	public String preUpload()throws Exception {
		Clazz c = this.clzDao.list(clz, null).get(0);
		request.setAttribute("clz", c);
		return "toUpload";
	}

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" enctype="multipart/form-data" method="post">
		<input type="hidden" name="cid" value="${clz.cid }">
		<input type="hidden" name="cname" value="${clz.cname }">
		<input type="hidden" name="cteachar" value="${clz.cteachar }">
		<!-- 注意该name值决定了Action的变量名称定义 -->
		<input type="file" name="file">
		<input type="submit" value="提交">
	</form>
</body>
</html>

注意:
必须为表单添加enctype值,并设置为:multipart/form-data。

配置struts.sy.xml

	<action name="/clz_*" class="com.xzy.web.ClazzAction" method="{1}">
			<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>

完成上面的之后我们在eclipse中修改tomcat服务器配置文件 添加服务器与真实目录映射关系

在server.xml中添加配置

<Context path="/My_strutr/upload" docBase="D:/upload/"/>

/My_strutr/upload-------服务器路径
D:/upload/-------本地映射路径

优化文件上传

上面用的是第三方的工具库代码,方法FileUtils.copyFile(srcFile, destFile)并不是很高效。我们通过添加缓冲区提高性能

	/**
	 * 自定义实现文件上传方法 注意这里带了缓存区
	 * @param source
	 * @param target
	 * @throws Exception
	 */
	public void copyBuffFile(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();
	}

拦截器

OneInterceptor

package com.li.Interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class OneInterceptor implements Interceptor{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7433091992916683936L;

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String intercept(ActionInvocation arg0) throws Exception {
		System.out.println("====OneInterceptor=====");
		String invoke = arg0.invoke();
		System.out.println("====OneInterceptor=====");
		return invoke;
	}
	
}


TwoInterceptor

package com.li.Interceptor;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;

public class TwoInterceptor implements Interceptor{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7433091992916683936L;

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public String intercept(ActionInvocation arg0) throws Exception {
		System.out.println("====TwoInterceptor=====");
		String invoke = arg0.invoke();
		System.out.println("====TwoInterceptor=====");
		return invoke;
	}
		
}

然后
在Struts的配置文件中配置一下拦截器

<!-- 定义这两个拦截器 -->
		<interceptors>
				<interceptor name="one" class="com.crud.web.OneInterceptor"></interceptor>
				<interceptor name="two" class="com.crud.web.TowInterceptor"></interceptor>	
		</interceptors>


	<action name="/clz_*" class="com.crud.web.ClazzAction" method="{1}">
			<interceptor-ref name="one"></interceptor-ref>
			<interceptor-ref name="two"></interceptor-ref>
	</action>


ojbk!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值