拦截器与文件上传
三种上传方案:
1、上传到tomcat服务器(不能及时刷新)
2、上传到指定文件目录,添加服务器与真实目录的映射关系,从而解耦上传文件与tomcat的关系
文件服务器(目前公司用的最常用的方法)
3、在数据库表中建立二进制字段,将图片存储到数据库(处理上百万的数据不方便)
完成图片上传:
写两个方法:1.上传图片2.跳转文件上传页面。
private File file;
private String fileContentType;
private String fileFileName;
/**
* 直接上传图片
* @return
* @throws IOException
*/
public String upload() throws IOException {
//注意:在Linux下是没有E盘的 linnx下只有一个盘符,那么意味着,当打包到linux服务器的时候需要改动代码
//这个时候通常是这么解决的,将targetPath对应目录串,配置到资源文件中,通过Properties类进行动态读取
//那么当需要将项目发布到Linux服务器的时候,只需要改变xxx.preperties文件中targetPath=/caoluo/img
//实际图片存储的位置
String targetDir="D:/杂七杂八/上传图片";
//存到数据库中的地址
String severPath="/upload";
FileUtils.copyFile(file, new File(targetDir+"/"+fileFileName));
//注意: 数据库存放是网络请求地址,而不是本地存放图片地址
clz.setPic(severPath+"/"+fileFileName);
this.clzDao.edit(clz);
return "toList";
}
/**
* 跳转文件上传页面
* @return
*/
public String preUpload() {
Clazz c=this.clzDao.list(clz, null).get(0);
request.setAttribute("clz", c);
return "toUpload";
}
struts_sy.xml配置
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
"http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
<package name="sy" extends="base" namespace="/sy">
<action name="/clz_*" class="com.web.ClazzAction" method="{1}">
<result name="list">/clzList.jsp</result>
<result name="preSave">/clzEdit.jsp</result>
<result name="toList" type="redirectAction">/clz_list</result>
<result name="toUpload">/upload.jsp</result>
</action>
</package>
</struts>
然后写一个上传图片的jsp页面
<body>
<form action="${pageContext.request.contextPath }/sy/clz_upload.action" method="post" enctype="multipart/form-data">
<input type="hidden" name="cid" value="${clz.cid }"> <br>
<input type="hidden" name="cname" value="${clz.cname }"> <br>
<input type="hidden" name="cteacher" value="${clz.cteacher }"> <br>
<!-- 注意:name对应的值决定了 自控制器action属性的命名 -->
<input type="file" name="file">
<input type="submit">
</form>
</body>
在server.xml中添加一行代码
<!-- Access log processes all example.
Documentation at: /docs/config/valve.html
Note: The pattern used is equivalent to using pattern="common" -->
<Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" pattern="%h %l %u %t "%r" %s %b" prefix="localhost_access_log" suffix=".txt"/>
<Context path="/T_caoluo/upload" docBase="D:/杂七杂八/上传图片/"/>
<Context docBase="T_caoluo" path="/T_caoluo" reloadable="true" source="org.eclipse.jst.jee.server:T_caoluo"/></Host>
</Engine>
</Service>
首页显示图片:
<c:forEach items="${clzList }" var="c">
<tr>
<td>${c.cid }</td>
<td>${c.cname }</td>
<td>${c.cteacher }</td>
<td>
<img alt="" style="width: 60px;height: 60px" src="${pageContext.request.contextPath }${c.pic }">
</td>
<td>
<a href="${pageContext.request.contextPath }/sy/clz_preSave.action?cid=${c.cid }">修改</a>
<a href="${pageContext.request.contextPath }/sy/clz_del.action?cid=${c.cid }">删除</a>
<a href="${pageContext.request.contextPath }/sy/clz_preUpload.action?cid=${c.cid }">上传图片</a>
</td>
</tr>
</c:forEach>
然后运行:
选择一个图片确认上传
效果图:
拦截器:
public class OneInterceptor implements Interceptor{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void init() {
// TODO Auto-generated method stub
}
@Override
public String intercept(ActionInvocation arg0) throws Exception {
System.out.println("======OneInterceptor======1");
String invoke=arg0.invoke();
System.out.println("=======OneInterceptor======2");
return invoke;
}
public class TwoInterceptor implements Interceptor{
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void init() {
// TODO Auto-generated method stub
}
@Override
public String intercept(ActionInvocation arg0) throws Exception {
System.out.println("======TwoInterceptor======1");
String invoke=arg0.invoke();
System.out.println("=======TwoInterceptor======2");
return invoke;
}
}
配置:
<package name="sy" extends="base" namespace="/sy">
<interceptors>
<interceptor name="one" class="com.interceptor.OneInterceptor"></interceptor>
<interceptor name="two" class="com.interceptor.TwoInterceptor"></interceptor>
</interceptors>
<action name="/clz_*" class="com.web.ClazzAction" method="{1}">
<interceptor-ref name="one"></interceptor-ref>
<interceptor-ref name="two"></interceptor-ref>
<result name="list">/clzList.jsp</result>
<result name="preSave">/clzEdit.jsp</result>
<result name="toList" type="redirectAction">/clz_list</result>
<result name="toUpload">/upload.jsp</result>
</action>
</package>
拦截成功