Interceptor
与filter的区别:先过filter再过interceptor
怎么设置拦截器:
在配置文件struts-sy.xml的
package里面配置
<!-- 配置2个拦截器 class指的是拦截器的全类名 -->
<interceptors>
<interceptor name="oneInterceptor" class="com.zking.five.interceptor.OneInterceptor"></interceptor>
<interceptor name="twoInterceptor" class="com.zking.five.interceptor.TwoInterceptor"></interceptor>
</interceptors>
<action name="interceptorAction" class="com.zking.five.web.InterceptorAction" >
<interceptor-ref name="oneInterceptor"></interceptor-ref>
<interceptor-ref name="twoInterceptor"></interceptor-ref>
</action>
<action name="interceptorAction" class="com.zking.five.web.InterceptorAction" >
<interceptor-ref name="oneInterceptor"></interceptor-ref>
<interceptor-ref name="twoInterceptor"></interceptor-ref>
</action>
文件上传:
文件上传的三种方案:
- 将上传的文件以二进制形式存放到数据库 OA系统 activity工作流程
- 将文件上传到文件服务器(硬盘足够大)中
- 将文件上传到tomcat所在的web服务器中
真实路径与虚拟路径的概念:
- 所谓的真实路径指的是在自己的电脑上能够看到的路径
- 所谓的虚拟路径,在自己的电脑上看不到的,路径在别人的电脑上能够看到
UploadAction :
package com.zking.five.web;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.zking.four.web.BeanAction;
public class UploadAction extends BeanAction {
private File file;//变量名指的是jsp的name属性,就是你要上传的文件
private String fileContentType;//xxxContentType
private String fileFileName;//xxxFileName
private String serverDiv = "/upload";//文件夹
//上传
public String upload() {
System.out.println(fileContentType);
System.out.println(fileFileName);
String realPath = getRealPath(serverDiv + "/"+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下的文件的所在位置
* @param path
* @return
*/
private String getRealPath(String path) {
// TODO Auto-generated method stub
return application.getRealPath(path);
}
//查看图片
public String openAs() {
String type = "image/jpeg";
String name = "u=2988065565,1735810570&fm=27&gp=0.jpg";
response.setContentType(type);//内容类型
response.setHeader("Content-Disposition","filename=" + name);//文件名
/*
* 将远程的图片输出到本地
* 数据源inputstream:远程 new file(realPath)
* 目的:输出到本地jsp response.getOutputStream()
*/
String realPath = getRealPath(serverDiv + "/"+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/jpeg";
String name = "u=2988065565,1735810570&fm=27&gp=0.jpg";
response.setContentType(type);
response.setHeader("Content-Disposition","attachment;filename=" + name);//文件名
/*
* 将远程的图片输出到本地
* 数据源inputstream:远程 new file(realPath)
* 目的:输出到本地jsp response.getOutputStream()
*/
String realPath = getRealPath(serverDiv + "/"+name);
try {
// FileUtils.copyFile(new File(realPath), response.getOutputStream());
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
BufferedOutputStream out = new BufferedOutputStream(response.getOutputStream());
copyStream(in, out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
private void copyStream(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();
}
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;
}
}
配置:
<action name="uploadAction_*" class="com.zking.five.web.UploadAction" method="{1}" >
<result name="success" >/success.jsp</result>
</action>
jsp页面demp1 :
<h1>Struts上传下载</h1>
<s:form namespace="/sy" action="uploadAction_upload" enctype="multipart/form-data" method="post" >
<input type="file" name="file" >
<input type="submit" value="上传" >
</s:form>
jsp页面success :
<h1>直接打开图片</h1>
<s:url var="openAsurl" namespace="/sy" action="uploadAction_openAs.action" ></s:url>
<s:property value="openAsurl" />
<img alt="" src='<s:property value="openAsurl" />'>
<h1>重载图片</h1>
<s:url var="downloadAsurl" namespace="/sy" action="uploadAction_download.action" ></s:url>
<s:a href="%{#downloadAsurl}" >下载</s:a>