Struts
Interceptor
implements Interceptor
extends AbstractInterceptor
与filter的区别:先过filter再过interceptor
org.apache.struts2.interceptor.FileUploadInterceptor
文件上传:
三种上传方案
-
上传到tomcat服务器 不推荐
上传后需要刷新一遍,图片才会出来
重启tomcat图片会丢失 -
上传到指定文件目录,添加服务器与真实目录的映射关系,从而解耦上传文件与tomcat的关系
文件服务器
图片上传到 d:/uploadImages/2019/08/23/20190823100951.png
访问:http://www.javaxl.com/uploadImages/2019/08/23/20190823100951.png
ip+portfile:///E:/temp/237/stationmaster_img.png /upload/7.png
-
在数据库表中建立二进制字段,将图片存储到数据库 淘汰
占用空间大
查询耗时长课程目标:图片上传以及页面展示
思路:
1、完成功能
2、两个底层
文件下载
另存为
直接打开
-
内容类型
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文件上传类型设置
根据struts2自带的fileupload拦截器中提供的allowedTypes来进行限制
<interceptor-ref name="fileUpload">
<param name="allowedTypes">image/png,image/gif,image/jpeg</param>
</interceptor-ref>
- 其它
enctype="multipart/form-data" method="post"
private File file;
private String fileContentType;
private String fileFileName;
clazzAction
package com.lrc.c.web;
import java.io.File;
import java.sql.SQLException;
import org.apache.commons.io.FileUtils;
import com.opensymphony.xwork2.ModelDriven;
import com.lrc.c.dao.ClzDao;
import com.lrc.c.entity.Clazz;
import com.lrc.c.util.BaseAction;
import com.lrc.c.util.PageBean;
public class ClazzAction extends BaseAction implements ModelDriven<Clazz>{
private ClzDao clzDao = new ClzDao();
private Clazz clz = new Clazz();
private File file;
private String fileFileName;
private String fileContentType;
/**
* 跳转上传界面
* @return
*/
public String preUpload() {
//int cid = clz.getCid();
//if(cid != 0) {
try {
this.result = clzDao.list(clz, null).get(0);
} catch (Exception e) {
e.printStackTrace();
}
//}
return "preUpload";
}
/**
* 处理上传发送的请求
* @return
*/
public String upload() {
//int cid = clz.getCid();
//if(cid != 0) {
//图片存放的真实目录
String realDie="C:/572/993/chen";
//定于服务器的请求路径 实现显示图片
String serverDir="/upload";
try {
FileUtils.copyFile(file, new File(realDie+"/"+fileFileName));
//设置图片路径
clz.setPic(serverDir+"/"+fileFileName);
this.clzDao.edit(clz);
} catch (Exception e) {
e.printStackTrace();
}
//}
return "toList";
}
/**
* 查询班级列表的方法
* @return
*/
public String list() {
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
try {
this.result = clzDao.list(clz, pageBean);
request.setAttribute("pageBean", pageBean);
} catch (Exception e) {
e.printStackTrace();
}
return "list";
}
/**
* 跳转编辑页面(新增编辑页面是同一张)
* @return
*/
public String preSave() {
// 什么时候跳转新增页面?什么时候跳转编辑页面?
// 区别在于跳编辑页面的时候需要传递cid到后台进行查询,并且将查询到的数据进行回显?
int cid = clz.getCid();
if(cid != 0) {
try {
this.result = clzDao.list(clz, null).get(0);
} catch (Exception e) {
e.printStackTrace();
}
}
return "preSave";
}
/**
* 增加班级信息
* @return
*/
public String add() {
try {
this.code = this.clzDao.add(clz);
} catch (Exception e) {
e.printStackTrace();
}
return "toList";
}
/**
* 修改班级信息
* @return
*/
public String edit() {
try {
this.code = this.clzDao.edit(clz);
} catch (Exception e) {
e.printStackTrace();
}
return "toList";
}
/**
* 删除班级信息
* @return
*/
public String del() {
try {
this.code = 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 getFileFileName() {
return fileFileName;
}
public void setFileFileName(String fileFileName) {
this.fileFileName = fileFileName;
}
public String getFileContentType() {
return fileContentType;
}
public void setFileContentType(String fileContentType) {
this.fileContentType = fileContentType;
}
}
claList.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="/lrc" prefix="z" %>
<!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>
<h2>小说目录</h2>
<br>
<form action="${pageContext.request.contextPath}/sy/clz_list.action"
method="post">
书名:<input type="text" name="bname">
<input type="submit"
value="确定">
</form>
<a href="${pageContext.request.contextPath}/sy/clz_preSave.action">新增</a>
<table border="1" width="100%">
<tr>
<td>编号</td>
<td>班级名称</td>
<td>教员</td>
<td>图片</td>
<td>操作</td>
</tr>
<c:forEach items="${result }" var="c">
<tr>
<td>${c.cid }</td>
<td>${c.cname }</td>
<td>${c.cteacher }</td>
<td>${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>
</table>
<z:page pageBean="${pageBean }"></z:page>
</body>
</html>
upload.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">
cid:<input type="hidden" value="${result.cid }" name="cid"><br>
cname:<input type="hidden" value="${result.cname }" name="cname"><br>
cteacher:<input type="hidden" value="${result.cteacher }" name="cteacher"><br>
<input type="file" name="file">
<input type="submit" value="ok">
</form>
</body>
</html>
struts-sy.xml
<action name="clz_*" class="com.lrc.c.web.ClazzAction" method="{1}">
<result name="list">/claList.jsp</result>
<result name="preSave">/clzEdit.jsp</result>
<result name="preUpload">/upload.jsp</result>
<result name="toList" type="redirectAction">/clz_list</result>
</action>
配置server.xml