Interceptor
implements :Interceptor
extends :BaseAction
与filter的区别:先过filter再过interceptor
org.apache.struts2.interceptor.FileUploadInterceptor
文件上传:
文件上传的三种方案
1、将上传的文件以二进制的形式存放到数据库 oa系统要用到activity工作流框架
2、将文件上传到文件服务器(硬盘足够大,CPU转速大)中
3、将文件上传到Tomcat所在的普通web服务器
真实路径与虚拟路径
1、真实路径:在自己电脑能够找到具体的盘符
2、虚拟路径:在自己电脑上是看不到的,路径在别人的电脑(tomcat服务器所在位置)上能够看到
public class StudentAction extends BaseAction implements ModelDriven<Student>{
private Student student=new Student();
private File file;
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>