文件上传与拦截器

1.文件上传

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

下面讲讲第二种
案例

package com.yiyni.crud.web;

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

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ModelDriven;
import com.yiyni.crud.dao.ClzDao;
import com.yiyni.crud.entity.Clazz;
import com.yiyni.crud.util.PageBean;

public class ClazzAction extends BaseAction implements ModelDriven<Clazz>{
	private ClzDao clzDao = new ClzDao(); 
	private Clazz clz = new Clazz();
//	img/img//imgContentType/imgFileName
	private File file;
	private String fileContentType;
	private String fileFileName;
	
	/**
	 * 单个查询
	 * @return
	 */
	public String list() {
		PageBean pageBean = new PageBean();
		pageBean.setRequest(request);
		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 upload() {
		try {
//			注意:在linux下是没有E盘的,linux下只有一个盘符name意味着,当打包到linux服务器的时候需要改动代码
//			这个时候通常是这么解决的,将targetPath对应目录,配置到资源文件中,通过properties类进行动态读取
//			那么当需要将项目发不到linux服务器的时候,只需啊哟改变XXXX。propertiesPath=/yinyi/struts/img
			//实际图片存的地址
			String targetDir ="E:/azp/temp";
			//存到数据库中的地址
			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 tafget
	 */
	public void copyBufFile(File source,File tafget){
		try {
			BufferedInputStream in = new BufferedInputStream(new FileInputStream(source));
			BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tafget));
			byte[] bbuf = new byte[1024];
			int len =0;
			while((len = in.read(bbuf))!=-1) {
				out.write(bbuf,0,len);
			}
			in.close();
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	
	}
	
	/**
	 * 跳转文件上传页面
	 * @return
	 */
	public String preUpload() { 
		try {
			Clazz c = this.clzDao.list(clz, null).get(0);
			request.setAttribute("clz", c);
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
				| SQLException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}	
		return "toUpload";
	}
	
	/**
	 * 跳转新增修改页面的公用方法
	 * @return
	 */
	public String preSave() {
		if(clz.getCid()!=0) {
			try {
				Clazz 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 (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
				| SQLException e) {
			e.printStackTrace();
		}	
		return "toList";
	}
	/**
	 * 修改
	 * @return
	 */
	public String edit() {
		try {
			this.clzDao.edit(clz);
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
				| SQLException e) {
			e.printStackTrace();
		}	
		return "toList";
	}
	/**
	 * 删除
	 * @return
	 */
	public String del() {
		try {
			this.clzDao.del(clz);
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
				| SQLException e) {
			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;
	}
	
}

上传界面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>

2.拦截器Interceptor

下面我们讲讲拦截器
一共两种
2.1:implements Interceptor
2.2:extends AbstractInterceptor

OneInterceptor

public class OneInterceptor implements Interceptor{

	@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========1");
		String invoke = arg0.invoke();
		System.out.println("========OneInterceptor========2");
		return invoke;
	}

}

TwoInterceptor

public class TwoInterceptor implements Interceptor{

	@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========1");
		String invoke = arg0.invoke();
		System.out.println("========TwoInterceptor========2");
		return invoke;
	}

}

本次的分享就到此结束了,感谢您的观看。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值