Interceptor
implements Interceptor
extends AbstractInterceptor
与filter的区别:先过filter再过interceptor
org.apache.struts2.interceptor.FileUploadInterceptor
文件上传:
三种上传方案
虚拟路径与真实路径 /upload
copyFile与copydirectory
文件下载
另存为
直接打开
package com.zking.five;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.zking.four.BaseAction;
/**
* 文件上传的三种方式:
* 1.上传的文件以二进制的形式保存到数据库(activiti,jbpmg工作流框架)oi系统
* 2.将文件上传到指定服务器的硬盘(cpu快,硬盘大);
* 3.将文件上传到tomcat所在服务器(对应的静态资源服务器)
* @author Administrator
*
* 真实路径和虚拟路径
*相对于自己的电脑而言的
*/
public class FileAction extends BaseAction {
// 这是jsp传递过来的具体文件
private File file;
private String fileFileName;
private String fileContentType;
// 虚拟路径
private String serverDir ="upload";
/**
* 上传图片到服务器
* @return
*/
public String upload() {
System.out.println("fileFileName"+fileFileName);
System.out.println("fileContentType"+fileContentType);
String realPath=getRealPath(serverDir+"/"+fileFileName);
System.out.println("realPath:"+realPath);
try {
FileUtils.copyFile(file, new File(realPath));
} catch (IOException e) {
e.printStackTrace();
}
return "seccess";
}
/**
* 文件夹的真实路径
* @param path
* @return
*/
private String getRealPath(String path) {
return this.request.getServletContext().getRealPath(path);
}
/**
* 下载图片
* @return
*/
public String saveAs() {
String fileName="QQ图片20181114120413.png";
String fileType="image/jpeg";
response.setContentType(fileType);
response.setHeader("Content-Disposition","attachment;filename=" + fileName);
String realPath=getRealPath(serverDir+"/"+fileName);
try {
FileUtils.copyFile(new File(realPath), response.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 直接在页面打开图片
* @return
*/
public String poenAs() {
String fileName="QQ图片20181114120413.png";
String fileType="image/jpeg";
response.setContentType(fileType);
response.setHeader("Content-Disposition","filename=" + fileName);
String realPath=getRealPath(serverDir+"/"+fileName);
try {
FileUtils.copyFile(new File(realPath), response.getOutputStream());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
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”
private File file;
private String fileContentType;
private String fileFileName;