三种上传方案
1、上传到tomcat服务器 上传图片的存放位置与tomcat服务器的耦合度太高
2、上传到指定文件目录,添加服务器与真实目录的映射关系,从而解耦上传文件与tomcat的关系文件服务器
3、在数据库表中建立二进制字段,将图片存储到数据库
(我所使用的是第二种方法)
根据我struts增删改查的基础上完成图片上传:https://blog.csdn.net/weixin_45180769/article/details/97827122
clazzAction
package com.ningjie.crud.web;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.io.FileUtils;
import com.ningjie.crud.dao.ClazzDao;
import com.ningjie.crud.entity.Clazz;
import com.ningjie.crud.util.BaseAction;
import com.ningjie.crud.util.PageBean;
import com.opensymphony.xwork2.ModelDriven;
public class ClazzAction extends BaseAction implements ModelDriven<Clazz>{
private Clazz clz=new Clazz();
private ClazzDao dao=new ClazzDao();
private File file;
private String fileFileName;
private String fileContentType;
public String list() {
PageBean pageBean=new PageBean();
pageBean.setRequest(request);
try {
List<Clazz> list=this.dao.list(clz, pageBean);
request.setAttribute("clzList",list);
request.setAttribute("pageBean",pageBean);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "list";
}
public String add() {
try {
this.dao.add(clz);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
| SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
public String del() {
try {
this.dao.del(clz);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
| SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
public String edit() {
try {
this.dao.edit(clz);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
| SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "toList";
}
public String preSave() {
if(clz.getCid()!=0) {
try {
this.result=this.dao.list(clz, null).get(0);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "preSave";
}
public String preUpload() {
if(clz.getCid()!=0) {
try {
this.result=this.dao.list(clz, null).get(0);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "preUpload";
}
public String upload() {
String dir="E:/a";
String server="/upload";
try {
// FileUtils.copyFile(file,new File(dir+"/"+fileFileName));
copyFile(file,new File(dir+"/"+fileFileName));
clz.setPic(server+"/"+fileFileName);
this.dao.edit(clz);
} catch (Exception e) {
// TODO: handle exception
}
return "toList";
}
/**
* 利用缓冲流技术进行拷贝
* @param source
* @param target
* @throws IOException
*/
public 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);
}
in.close();
out.close();
}
@Override
public Clazz getModel() {
// TODO Auto-generated method stub
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;
}
}
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="/hello_*" class="com.ningjie.web.HelloAction" method="{1}">
<result name="success">/success.jsp</result>
</action>
<action name="/clz_*" class="com.ningjie.crud.web.ClazzAction" method="{1}">
<result name="list">/clzList.jsp</result>
<result name="preUpload">/clzUpload.jsp</result>
<result name="preSave">/clzEdit.jsp</result>
<result name="toList" type="redirectAction">clz_list</result>
</action>
</package>
</struts>
写一个专门用来进行图片上传的jsp:clzUpload.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>Insert title here</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/sy/clz_upload.action" method="post" enctype="multipart/form-data">
<input type="hidden" name="cid" value="${result.cid }"><br>
<input type="hidden" name="cname" value="${result.cname}"><br>
<input type="hidden" name="cteacher" value="${result.cteacher }"><br>
<input type="file" name="file">
<input type="submit" value="提交">
</form>
</body>
</html>
clzList.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="/zking" prefix="z" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!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>Insert title here</title>
</head>
<body>
<h2>小说目录</h2>
<br>
<form action="${pageContext.request.contextPath}/sy/clz_list.action"
method="post">
书名:<input type="text" name="cname"> <input type="submit"
value="确定">
<!--
<input type="hidden" name="rows" value="10">
<input type="hidden" name="page" value="2">
<input type="hidden" name="pagination" value="false">-->
</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="${clzList }" var="b">
<tr>
<td>${b.cid }</td>
<td>${b.cname }</td>
<td>${b.cteacher }</td>
<td>
<img style="width: 70px;height: 70px;" src="${pageContext.request.contextPath}${b.pic }">
</td>
<td>
<a href="${pageContext.request.contextPath}/sy/clz_preSave.action?cid=${b.cid }">修改</a>
<a href="${pageContext.request.contextPath}/sy/clz_preUpload.action?cid=${b.cid }">图片上传</a>
<a href="${pageContext.request.contextPath}/sy/clz_del.action?cid=${b.cid }">删除</a>
</td>
</tr>
</c:forEach>
</table>
<z:page pageBean="${pageBean}"></z:page>
</body>
</html>
在系统server中配置server.xml
效果展示: