文件上传
文件上传的三种方式:
1、上传到tomcat服务器
2、上传到指定文件目录,添加服务器与真实目录的映射关系,从而解耦上传文件与tomcat的关系
文件服务器
3、在数据库表中建立二进制字段,将图片存储到数据库
第一二种是常用的,而第三种搞那种数据少时可以用
文件上传实例(这里我们用第二种上传方式来实现)
ClazzAction
public class ClazzAction extends BaseAction implements ModelDriven<Clazz> {
private ClazzDao clzDao = new ClazzDao();
private Clazz clz = new Clazz();
private File file;
private String fileContentType;
private String fileFileName;
/**
* 查询所有
* @return
*/
public String list() {
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
try {
List<Clazz> list = this.clzDao.list(clz, pageBean);
// this.result = this.clzDao.list(clz, pageBean);
request.setAttribute("clzList", list);
request.setAttribute("pageBean", pageBean);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
e.printStackTrace();
}
return "list";
}
/**
* 直接上传
* @return
*/
public String upload() {
try {
//注意:在Linux下是没有E盘的,Linux下只有一个盘符,那么意味着,当打包到Linux服务器的时候需要改动代码
//这个时候通常是这么解决的,将targetPath对应目录串,配置到资源文件中,通过Properties类进行动态读取
//那么当需要将项目发布到Linux服务器的时候,只需要改变xxx.properties文件中targetPath=/zking/T224/img
//实际图片存储位置
String targetDir = "D:/背景";
//存到数据库中的地址
String severPath = "/upload";
// FileUtils.copyFile(file, new File(targetDir+"/"+fileFileName));
copyFile(file, new File(targetDir+"/"+fileFileName));
//注意:数据库存放时网络请求地址,而不是本地图片存放的地址
clz.setPic(severPath+"/"+fileFileName);
this.clzDao.edit(clz);
} catch (Exception e) {
e.printStackTrace();
}
return "toList";
}
/**
* FileUtils.copyFile的底层,并且通过缓冲区进行了增强
* @param source
* @param target
* @throws Exception
*/
public void copyFile(File source,File target) throws Exception{
BufferedInputStream in = new BufferedInputStream(new FileInputStream(source));
BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(target));
byte[] bbuf = new byte[1024];
int len = 0;
while((len = in.read(bbuf))!=-1) {
out.write(bbuf, 0, len);
}
in.close();
out.close();
}
/**
* 跳转文件上传页面
* @return
*/
public String preUpload() {
try {
Clazz c = this.clzDao.list(clz, null).get(0);
request.setAttribute("clz",c);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
e.printStackTrace();
}
return "toUpload";
}
/**
* 跳转新增修改页面的通用方法
*
* @return
*/
public String preSave() {
if(clz.getCid()!=0) {
try {
Clazz c = this.clzDao.list(clz, null).get(0);
request.setAttribute("clz",c);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
e.printStackTrace();
}
}
return "preSave";
}
/**
* 新增
* @return
*/
public String add() {
try {
result = this.clzDao.add(clz);
} catch (Exception e) {
e.printStackTrace();
}
return "toList";
}
/**
* 修改
* @return
*/
public String edit() {
try {
this.clzDao.edit(clz);
} catch (Exception e) {
e.printStackTrace();
}
return "toList";
}
/**
* 删除
* @return
*/
public String del() {
try {
this.clzDao.del(clz);
} catch (Exception e) {
e.printStackTrace();
}
return "toList";
}
@Override
public Clazz getModel() {
return clz;
}
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;
}
}
配置好struts-sy.xml
<action name="/clz_*" class="com.tang.crud.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>
jsp界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>图片上传</title>
</head>
<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>
</html>
在你的本地servers的server.xml加以下一条代码
<Context path="struts/upload" docBase="E:/temp/T224/"/>
效果展示:
拦截器Interceptor(两种方式)
Struts2拦截器原理
Struts2拦截器的实现原理相对简单,当请求struts2的action时,Struts2会查找配置文件,并根据其配置实例化相对应的拦截器对象,然后串成一个列表,最后一个一个地调用列表中的拦截器
OneInterceptor拦截器
package com.lst.crud.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
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;
}
}
TwoInterceptor拦截器
package com.lst.crud.interceptor;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor;
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("=============TwoInterceptor=============1");
String invoke = arg0.invoke();
System.out.println("=============TwoInterceptor=============2");
return invoke;
}
}
在Struts的配置文件中配置一下拦截器
<struts>
<!-- 多了一个包的概念,区分分类 -->
<package name="sy" extends="base" namespace="/sy">
<!-- 定义这两个拦截器 -->
<interceptors>
<interceptor name="one" class="com.crud.web.OneInterceptor"></interceptor>
<interceptor name="two" class="com.crud.web.TowInterceptor"></interceptor>
<!-- 配置拦截器栈 -->
<interceptor-stack name="myStack">
<!-- 默认自带的拦截器,当配置自己的拦截器时不再走默认的拦截器,所以需要调用自带的拦截器,并写在第一行 主要这是关键,如果不加 defaultStack拦截器就会导致from表达提交值失效-->
<interceptor-ref name="defaultStack"/>
<!-- 加入自己的拦截器 -->
<interceptor-ref name="one"/>
<interceptor-ref name="two"/>
</interceptor-stack>
</interceptors>
<action name="/clz_*" class="com.crud.web.ClazzAction" method="{1}">
<!-- 引入拦截器栈,只要请求clz的相关方法就会触发拦截栈中定的拦截器 -->
<interceptor-ref name="myStack"></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>
</struts>
运行效果