Interceptor
implements Interceptor
extends AbstractInterceptor
与filter的区别:先过filter再过interceptor
1:所有拦截器都使用接口Interceptor ,Action去实现这个接口;
Init()方法:在服务器起动的时候加载一次,并且只加载一次;
Destroy()方法:当拦截器销毁时执行的方法;
Interceptor()方法:其中里边有一个参数invocation;
public String intercept(ActionInvocation invocation) throws xception {
System.out.println("调用的action类是:"+invocation.getAction().getClass().getName());
System.err.println("调用的action方法是:"+invocation.getProxy().getActionName());
System.out.println("调用的方法是:"+invocation.getProxy().getMethod());
return invocation.invoke();
}
org.apache.struts2.interceptor.FileUploadInterceptor
文件上传:
三种上传方案
虚拟路径与真实路径 /upload
copyFile与copydirectory
文件上传的三种方式
-
1、上传的图片以二进制的形式保存到数据库(activiti、jbpm工作流框架)oa系统
-
2、将文件上传到指定服务器的硬盘
-
3、将文件上传到tomacat所在服务器
- 文件下载
另存为
直接打开
- 文件下载
public class FileAction extends BaseAction{
private File file;
private String fileFileName;
private String fileContentType;
private String serverDir="/upload";
/**
* 文件上传
* @return
* @throws IOException
*/
public String upload() throws IOException {
String realPath=getRealPath(serverDir + "/" + fileFileName);
System.out.println("路径"+realPath);
FileUtils.copyFile(file, new File(realPath));
return "success";
}
/**
* 获取文件的真实路径
* @param path
* @return
*/
private String getRealPath(String path) {
return this.request.getServletContext().getRealPath(path);
}
/**
* 直接在页面打开图片
* @return
* @throws IOException
*/
public String openAs() throws IOException {
String fileName="stationmaster_img.png";
String fileType="image/png";
//设置文件类型
response.setContentType(fileType);
response.setHeader("Content-Disposition","filename=" + fileName);
String realPath=getRealPath(serverDir + "/" + fileName);
//从服务器获取图片写到本地
//FileUtils.copyFile(new File(realPath), response.getOutputStream());
BufferedInputStream in=new BufferedInputStream(new FileInputStream(realPath));
BufferedOutputStream out=new BufferedOutputStream(response.getOutputStream());
copyBufStream(in, out);
return null;
}
/**
* 如果没有导入FileUtils文件的jar包就可以用自定义的方法来获取图片写到本地
* @param in
* @param out
* @throws IOException
*/
private void copyBufStream(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();
}
/**
* 下载图片
* @return
* @throws IOException
*/
public String saveAs() throws IOException {
String fileName="stationmaster_img.png";
String fileType="image/png";
//设置文件类型
response.setContentType(fileType);
response.setHeader("Content-Disposition","attachment;filename=" + fileName);
String realPath=getRealPath(serverDir + "/" + fileName);
//从服务器获取图片写到本地
FileUtils.copyFile(new File(realPath), response.getOutputStream());
return null;
}
public File getFile() {
return file;
}
public void setFile(File file) {
this.file = file;
}
public String getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
}
-
内容类型
response.setContentType(d.getMime()); -
设置响应头
response.setHeader(“Content-Disposition”,“attachment;filename=” + fileName);//文件名 -
处理文件名的中文乱码
String fileName = d.getFileName();
fileName = new String(fileName.getBytes(“utf-8”), “iso8859-1”); -
struts2文件上传大小设置
-
struts2文件上传类型设置
image/png,image/gif,image/jpeg
根据struts2自带的fileupload拦截器中提供的allowedTypes来进行限制
-
其它
enctype=“multipart/form-data” method=“post”
//在前台jsp页面嵌入form表单
<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/helloworld.action" method="post">
<input type="file"name="uploadImage"><br/>
<button type="submit">Submit</button>
</form>