struts2拦截器Interceptor与文件上传

Interceptor
让类implements Interceptor接口
与filter的区别:先过filter再过
Interceptor 类

package com.zking.five;

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

public class OneInterceptor implements Interceptor{

	private static final long serialVersionUID = 1L;

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		System.out.println("OneInterceptor----destroy");
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub
		System.out.println("OneInterceptor----init");
	}

	@Override
	public String intercept(ActionInvocation arg0) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("走");
		return arg0.invoke();
	}

}

使用拦截器要在struts.xml文件里配置
interceptors 必须在第一行

<package name="sy" extends="base" namespace="/sy">
		<interceptors>
			<interceptor name="oneInterceptor" class="com.zking.five.OneInterceptor"></interceptor>
			<interceptor name="twoInterceptor" class="com.zking.five.TwoInterceptor"></interceptor>
		</interceptors>
		<!-- method:当不填的时候,默认调用execute方法;如果填了,动态调用指定的方法 -->
		<action name="claAction_*" class="com.zking.one.ClaAction" method="{1}">
			<result name="rs">/success.jsp</result>
		</action>
		<action name="interceptorAction" class="com.zking.five.InterceptorAction">
			<interceptor-ref name="oneInterceptor"></interceptor-ref>
			<interceptor-ref name="twoInterceptor"></interceptor-ref>
		</action>
		<action name="uploadAction_*" class="com.zking.five.UploadAction" method="{1}">
			<result name="success" type="redirect">/sy/studentAction_list.action</result>
		</action>
	</package>

文件上传要注意到的点

  1. 内容类型
    response.setContentType(d.getMime());

  2. 设置响应头
    response.setHeader(“Content-Disposition”,“attachment;filename=” + fileName);//文件名

  3. 处理文件名的中文乱码
    String fileName = d.getFileName();
    fileName = new String(fileName.getBytes(“utf-8”), “iso8859-1”);

  4. struts2文件上传大小设置

  5. struts2文件上传类型设置
    根据struts2自带的fileupload拦截器中提供的allowedTypes来进行限制

    image/png,image/gif,image/jpeg
  6. 其它
    form表单的属性
    enctype=“multipart/form-data” method=“post”
    private File file;
    private String fileContentType;
    private String fileFileName;

文件上传代码

package com.zking.five;

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.BaseAction;
import com.zking.test.dao.StudentDAO;
import com.zking.test.entity.Student;

/**
 * 文件上传三种方式:
 * 1、将文件以二进制的形式保存到数据库中 activiti工作流框架 
 * 2、将文件存储到专门文件服务器(存放文件用的Linux系统)中
 * 3、直接将文件存储到服务器(tomcat所在服务器)中
 *
 */
public class UploadAction extends BaseAction implements ModelDriven<Student>{

	private Student student=new Student();
	private File file;
	private String fileContentType;
	private String fileFileName;
	
	private String serverDir="/upload";
	
	/**
	 * 上传
	 * @return
	 */
	public String upload() {
		String realPath=getRealPath(serverDir+"/"+fileFileName);
		try {
			FileUtils.copyFile(file, new File(realPath));
			try {
				new StudentDAO().upImg(student, fileContentType, fileFileName);
			} catch (InstantiationException | IllegalAccessException | NoSuchFieldException | SecurityException
					| SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}

	/**
	 * 获取Linux目录的真实路径
	 * @param path:指的是本地路径(相对于工程WebContent所在的路径)
	 * @return
	 */
	private String getRealPath(String path) {
		return application.getRealPath(path);
	}
	
	/**
	 * 打开图片
	 * @return
	 */
	public String openAs() {
//		String type="image/jpeg";
//		String name="5.jpg";
		String type=request.getParameter("type");
		String name=request.getParameter("name");
		response.setContentType(type);
		response.setHeader("Content-Disposition","filename=" + name);//文件名
		/*
		 * 从服务器拿图片到本地
		 * 源:服务器
		 * 目的:jsp输出图片
		 */
		File servletFile=new File(getRealPath(serverDir + "/" + name));
		try {
			FileUtils.copyFile(servletFile, response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	/**
	 * 下载图片
	 * @return
	 */
	public String saveAs() {
//		String type="image/jpeg";
//		String name="5.jpg";
		String type=request.getParameter("type");
		String name=request.getParameter("name");
		response.setContentType(type);
		response.setHeader("Content-Disposition","attachment;filename=" + name);//文件名
		File servletFile=new File(getRealPath(serverDir + "/" + name));
		try {
//			FileUtils.copyFile(servletFile, response.getOutputStream());
			BufferedInputStream bit=new BufferedInputStream(new FileInputStream(servletFile));
			BufferedOutputStream bos=new BufferedOutputStream(response.getOutputStream());
			copyByBuffered(bit, bos);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	private void copyByBuffered(BufferedInputStream in, BufferedOutputStream out) throws IOException {
		byte[] bt=new byte[1024];
		int len=0;
		while((len=in.read(bt))!=-1) {
			out.write(bt, 0, len);
		}
		in.close();
		out.close();
	}

	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;
	}
	
}

猜你喜欢

转载自blog.csdn.net/qq_41277773/article/details/83065438