拦截器与文件上传

一,拦截器
1.Interceptor
implements Interceptor
extends AbstractInterceptor
与filter的区别:先过filter再过interceptor

org.apache.struts2.interceptor.FileUploadInterceptor
2.在启动tomcat服务时就初始化了。

package com.zking.five.interceptor;

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

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ModelDriven;
import com.zking.four.web.BaseAction;
import com.zking.test.dao.StudentDAO;
import com.zking.test.entity.Student;
/**
 * 文件上传的三种方案
 * 1.将上传的文件以二进制的形式存放到数据库   oa系统 activiti工作流框架
 * 2.将文件上传到文件服务器中   特点:硬盘足够大,cpu转速快
 * 3.将文件上传到tomcat所在的普通web服务器
 * 
 * 真实路径与虚拟路径的概念
 * 1.所谓真实路径指的是在自己电脑上能够找到的路径
 * 2.所谓虚拟,在自己电脑上是看不到的,路径在别人的电脑上(tomcat所在位置)能够看到
 * @author pc
 *
 */

public class UploadAction extends BaseAction implements ModelDriven<Student>{
	
	private Student student = new Student();
	private StudentDAO studentdao = new StudentDAO();
	
	private File file;//变量名指的是jsp的name属性,就是你要上传文件     xxx
	private String fileContentType;//xxxContentType
	private String fileFileName;//xxxFileName
	
	private String serverDir = "/upload";
	
	
	
	public String upload() {
//		System.out.println(fileContentType);
//		System.out.println(fileFileName);
		//真实路径指的是Linux的上传文件所在位置
		String realPath = getRealPath(serverDir + "/" + fileFileName);
		System.out.println(realPath);
		/**
		 * 参数一:本地文件图片
		 * 参数二:在服务器生成的文件
		 */
		try {
			FileUtils.copyFile(file, new File(realPath));
			student.setImgtype(fileContentType);
			student.setImgname(fileFileName);
			try {
				this.studentdao.upload(student);
			} catch (InstantiationException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
	/**
	 *获取的是Linux的上传文件所在位置
	 * @param path
	 * @return
	 */
	private String getRealPath(String path) {
		// TODO Auto-generated method stub
		return appication.getRealPath(path);
	}

	public String openAs() {
//		String type = "UploadAction.java";
//		String name = "548e6ced7a4c0.jpg";
//		response.setContentType(type);
		response.setContentType(student.getImgtype());
		response.setHeader("Content-Disposition","filename=" + student.getImgname());
		/**
		 * 将远程的图片输出到本地
		 * 数据inputStream:远程  new file(realPath)
		 * 目的:输出到本地的jsp  respons.getoupStream
		 */
//		String realPath = getRealPath(serverDir + "/" + name);
		String realPath = getRealPath(serverDir + "/" + student.getImgname());
		try {
			//上传图片
			FileUtils.copyFile(new File(realPath), response.getOutputStream());
			//上传视频
//			BufferedInputStream in = new BufferedInputStream(new FileInputStream(new File(realPath)));
//			BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
//			copyStream(in, out);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
//	private void copyStream(BufferedInputStream in,BufferedOutputStream out) throws IOException {
//		byte[] bbuf = new byte[1024];
//		int len = 0;
//		while((len = in.read(bbuf))!=-1) {
//			out.write(bbuf,0,len);
//		}
//		in.close();
//		out.close();
//	}
	
	
	public String download() {
//		String type = "UploadAction.java";
//		String name = "548e6ced7a4c0.jpg";
		response.setContentType(student.getImgtype());
		response.setHeader("Content-Disposition","attachment;filename=" + student.getImgname());
		/**
		 * 将远程的图片输出到本地
		 * 数据inputStream:远程  new file(realPath)
		 * 目的:输出到本地的jsp  respons.getoupStream
		 */
		String realPath = getRealPath(serverDir + "/" + student.getImgname());
		try {
			FileUtils.copyFile(new File(realPath), response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return null;

	}

	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;
	}
	@Override
	public Student getModel() {
		// TODO Auto-generated method stub
		return student;
	}
	
	
	
	
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值