拦截器与文件上传,下载,展示

Interceptor

implements :Interceptor
extends :BaseAction
与filter的区别:先过filter再过interceptor
org.apache.struts2.interceptor.FileUploadInterceptor

文件上传:

文件上传的三种方案

  • 1、将上传的文件以二进制的形式存放到数据库 oa系统要用到activity工作流框架
  • 2、将文件上传到文件服务器(硬盘足够大,CPU转速大)中
  • 3、将文件上传到Tomcat所在的普通web服务器

真实路径与虚拟路径

  • 1、真实路径:在自己电脑能够找到具体的盘符
  • 2、虚拟路径:在自己电脑上是看不到的,路径在别人的电脑(tomcat服务器所在位置)上能够看到
//在Action里
public class StudentAction extends BaseAction implements ModelDriven{
	private Student student=new Student();
	private File file;//与jsp页面的name对应
	private String fileContentType;
	private String fileFileName;
	private String serverDir="/upload";
	
	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;
	}

	public String upload() {
		System.out.println(fileContentType);
		System.out.println(fileFileName);
		String realPath=getRealPath(serverDir+"/"+fileFileName);
		try {
			FileUtils.copyFile(file, new File(realPath));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			String sid = (String) session.getAttribute("sid");
			student.setSid(Integer.parseInt(sid));
			student.setImagtype(fileContentType);
			student.setImagname(fileFileName);
			studentDAO.imagAdd(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();
		}
		return SUCCESS;
	}
	
	public String toImag() {
		String sid=request.getParameter("sid");
		session.setAttribute("sid", sid);
		return Img;
	}

	private String getRealPath(String path) {
		// TODO Auto-generated method stub
		return application.getRealPath(path);
	}
	
	public String download() {
		String type=student.getImagtype();
		String name=student.getImagname();
		response.setContentType(type);
		response.setHeader("Content-Disposition","attachment;filename=" + name);//文件名
		/**
		 * 将远程的图片输出到本地
		 * 数据源inputstream:远程 	new file(realPath)
		 * 目的:输出到本地的jsp response.getoutpStream
		 */
		String realPath=getRealPath(serverDir+"/"+name);
		try {
			FileUtils.copyFile(new File(realPath), response.getOutputStream());
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
//在upload.jsp里

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ include file="/jsp/common/head.jsp" %>




Insert title here



	
	



文件上传

	
			
			
			上传

另存为


			
			
			
			下载

直接打开


				" style="width: 150px;height: 150px">
			

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. 其它

enctype=“multipart/form-data” method=“post”
private File file;
private String fileContentType;
private String fileFileName;

你可能感兴趣的:(Struts)