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

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<Student>{
	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" %>
<!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>
<s:form action="studentAction_upload" namespace="/sy" method="post" enctype="multipart/form-data">
	<input type="file" name="file">
	<input type="submit" value="上传">
</s:form>
</body>
</html>

文件上传

	<s:url namespace="/sy" action="studentAction_toImag" var="toImg">
			<s:param name="sid" value="#s.sid"></s:param>
			</s:url>
			<s:a href='%{toImg}'>上传</s:a>

另存为

<s:url var="downloadUrl" action="studentAction_download.action" namespace="/sy">
			<s:param name="imagtype" value="#s.imagtype"></s:param>
			<s:param name="imagname" value="#s.imagname"></s:param>
			</s:url>
			<s:a href="%{#downloadUrl}">下载</s:a>

直接打开

<td>
				<img alt="" src="http://localhost:8080/1009_struts_base/upload/<s:property value="#s.imagname"/>" style="width: 150px;height: 150px">
			</td>

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;

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值