Stuts2 文件上传
文件上传:
三种上传方案
-
上传到tomcat服务器
不推荐 上传后需要刷新一遍,图片才会出来重启tomcat图片会丢失 -
上传到指定文件目录,添加服务器与真实目录的映射关系,从而解耦上传文件与tomcat的关系
文件服务器
图片上传到 d:/uploadImages/2019/08/23/20190823100951.png
访问:http://www.javaxl.com/uploadImages/2019/08/23/20190823100951.png
ip+port -
在数据库表中建立二进制字段,将图片存储到数据库 淘汰
占用空间大
查询耗时长
演示
图片上传
在上一篇博客 Struts2(增删改查)中加入上传图片 https://blog.csdn.net/wx1762813417/article/details/100033035
clzList.jsp 中插入一行代码,携带Id 转发到 preUpload.jsp 中 进行文件上传
<a href="${pageContext.request.contextPath}/sy/clz_preUpload.action?cid=${c.cid}">上传</a>
新建一个上传图片的jsp页面
preUpload
<%@ 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>
<!-- 多功能表单 enctype="multipart/form-data" -->
<form action="${pageContext.request.contextPath}/sy/clz_upload.action" enctype="multipart/form-data" method="post">
<input type="hidden" value="${result.cid}" name="cid"/><br>
<input type="hidden" value="${result.cname}" name="cname"/><br>
<input type="hidden" value="${result.cteacher}" name="cteacher"/><br>
<input type="file" name="file"/><br>
<button type="submit">确认</button>
</form>
</body>
</html>
ClazzAction.java
package com.hyf.three.web;
import java.io.File;
import java.io.IOException;
import org.apache.commons.io.FileUtils;
import com.hyf.three.dao.ClazzDao;
import com.hyf.three.entity.Clazz;
import com.hyf.three.util.BaseAction;
import com.hyf.three.util.PageBean;
import com.opensymphony.xwork2.ModelDriven;
public class ClazzAction extends BaseAction implements ModelDriven<Clazz> {
private ClazzDao cd = new ClazzDao();
private Clazz c = new Clazz();
private File file; // <input type="file" name="file"/><br> name 要一致
private String fileFileName; // picFileName 中pic 要与 File pic 相同
private String fileContentType;
/**
* 查询班级所有信息
* @return
*/
public String list() {
PageBean pageBean = new PageBean();
pageBean.setRequest(request);
try {
this.result=this.cd.list(c, pageBean);
request.setAttribute("pageBean", pageBean);
} catch (Exception e) {
e.printStackTrace();
}
return "list";
}
/**
* 图片上传 跳页面
* @return
*/
public String preUpload() {
try {
this.result=this.cd.list(c, null).get(0);
} catch (Exception e) {
e.printStackTrace();
}
return "preUpload";
}
public String upload() {
// 图片存放的真实目录
String realDir = "D:/Images/1";
// 定义服务器的请求路径
String serverDir = "/upload";
try {
// 将本地图片上传到服务器的某一真实存在的目录
FileUtils.copyFile(file, new File(realDir + "/" + fileFileName));
c.setPic(serverDir+"/"+fileFileName);
this.cd.edit(c);
} catch ( Exception e) {
e.printStackTrace();
}
return "toList";
}
public String preSave() {
int cid =c.getCid();
if(cid !=0) {
try {
this.result=this.cd.list(c, null).get(0);
} catch (Exception e) {
e.printStackTrace();
}
}
return "preSave";
}
public String add() {
try {
this.code = this.cd.add(c);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
public String edit() {
try {
this.code = this.cd.edit(c);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
public String del() {
try {
this.code = this.cd.del(c);
} catch (Exception e) {
e.printStackTrace();
}
return "toList";
}
public Clazz getModel() {
return c;
}
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;
}
}
struts-sy.xml
<result name="preUpload">/preUpload.jsp</result>
<result name="toList" type="redirectAction">clz_list</result>
找到 server.xml 添加映射关系
<!-- 添加映射关系 -->
<Context docBase="D:/Images/1/" path="/web_struts/upload"/>
它与 clazzAction.java 文件的关系
clzList.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="/hyf" prefix="h"%>
<!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">
<!--用户设置查询 一页20条记录 -->
<!-- <input type="hidden" name="rows" value="20"/> -->
<!--用户设置不分页 -->
<!-- <input type="hidden" name="pagination" value="false"/> -->
书名:<input type="text" name="cname"> <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 width="200px" height="200px"><img width="200px" height="200px" 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>
</table>
<h:page pageBean="${pageBean}"></h:page>
</body>
</html>
图片上传成功啦
自己定义这个方法
/**
* 从本地文件写入到服务器
*
* @param source
* 本地文件
* @param target
* 目标
* @throws IOException
*/
private void copyFile(File source, File target) throws IOException {
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);
}
out.close();
in.close();
}