一、拦截器(Interceptor) 拦截器是动态拦截Action调用的对象,拦截器将Action共用的行为独立出来,在Action执行前后执行。这也就是我们所说的AOP,它是分散关注的编程方法,它将通用需求功能从不相关类之中分离出来;同时,能够共享一个行为,一旦行为发生变化,不必修改很多类,只要修改这个行为就可以。写action方法要继承Interceptor,要有三个方法
public class OneInterceptor implements Interceptor{
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
public void destroy() {//销毁 关闭服务器是运行
// TODO Auto-generated method stub
}
@Override
public void init() {//初始化 开启服务器是运行
// TODO Auto-generated method stub
System.out.println("OneInterceptor=======init");
}
@Override
public String intercept(ActionInvocation arg0) throws Exception {
System.out.println("OneInterceptor=======intercept");
return arg0.invoke();//要是上面的方法通过,则放行
}
}
在Struts.sy.xml配置时,要配置interceptor
<interceptorname="oneInter"class=“com.zking.struts.four.OneInterceptor”>
<interceptorname="twonter"class=“com.zking.struts.four.TwoInterceptor”>
strut文件上传
public class UploadAction extends BaseAction { private File file ;//指jsp的name属性,要上传的文件 xxx private String fileContentType;//文件的类型 xxxContentType private String fileFileName;//文件名 xxxFileNameprivate String serverDir = "/upload";//创建一个文件存放图片
public String upload() {//上传
System.out.println(fileContentType);
System.out.println(fileFileName);
String realPath = getRealPath(serverDir +"/" +fileFileName);
System.out.println(realPath);
//参数1:本地文件 参数2:在服务器上生成的文件
try {
FileUtils.copyFile(file, new File(realPath));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SUCCESS;
}
//真实路径指Linux下的上传文件的具体所在位置
private String getRealPath(String path) {
return application.getRealPath(path);
}
public String openAs() {//展示图片
//这是我定死的数据
String type="image/gif";
String name = "2.gif";
response.setContentType(type);//设置打开图片是什么格式的
//响应头
response.setHeader("Content-Disposition","filename=" + name);//文件名
/**
* 将远程的图片输出到本地
* 数据源inputStream :远程 new File(realPath)
* 目的:输出到本地的jsp response.getoutStream
*/
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;
}
public String download() {//下载
String type="image/gif";
String name = "2.gif";
response.setContentType(type);//设置打开图片是什么格式的
//响应头
response.setHeader("Content-Disposition","attachment;filename=" + name);//文件名
/**
* 将远程的图片输出到本地
* 数据源inputStream :远程 new File(realPath)
* 目的:输出到本地的jsp response.getoutStream
*/
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;
}
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;
}
}
注意的点:1.name属姓的file要在Action找中的file一致,不然会报Struts Problem Reportjava.lang.NullPointerException: Source must not be null ----这样的错误2.enctype=“multipart/form-data” 这是在form表单里的,是将文件以二进制的形式上传,这样可以实现多种类型的文件上传。3.attachment是附件;押扣的意思。在展示时是不用在响应头里面写的,而要在下载图片的时候加上,不然当点击下载时会指出现图片,而不会下载。4、记得要将action方法中的的三个属性写上set/get方法,不然会获取不到值。