struts2图片上传

struts2图片上传

三种上传方案
1、上传到tomcat服务器 缺点:上传图片的存放位置与tomcat服务器的耦合度太高,不常用
2、在数据库表中建立二进制字段,将图片存储到数据库,缺点:直接把图片存放到了数据库,太占取空间
3、上传到指定文件目录,添加服务器与真实目录的映射关系,从而解耦上传文件与tomcat的关系

ClazzAction(控制器)
代码:


public class ClazzAction extends BaseAction implements ModelDriven<Clazz> {
	private Clazz clz = new Clazz();
	private ClazzDao clzDao = new ClazzDao();
//	这里的属性名要和表单里的name对应	xxx
	private File file;
//	xxxFileName
	private String fileFileName;
//	xxxContentType
	private String fileContentType;
	
	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;
	}

	public String list() {
		PageBean pageBean = new PageBean();
		pageBean.setRequest(request);
		try {
			List<Clazz> list = this.clzDao.list(clz, pageBean);
			request.setAttribute("clzList", list);
			request.setAttribute("pageBean", pageBean);
		} catch (InstantiationException | IllegalAccessException | SQLException e) {
			e.printStackTrace();
		}
		return "list";
	}

	public String preSave() {
		if (clz.getCid() != 0) {
			try {
				this.result = this.clzDao.list(clz, null).get(0);
			} catch (InstantiationException | IllegalAccessException | SQLException e) {
				e.printStackTrace();
			}
		}
		return "preSave";
	}

	// preUpload
	/**
	 * 跳转上传图片的界面
	 * @return
	 */
	public String preUpload() {
		try {
			this.result = this.clzDao.list(clz, null).get(0);
		} catch (InstantiationException | IllegalAccessException | SQLException e) {
			e.printStackTrace();
		}
		return "preUpload";
	}
	
	public String upload() {
		String realDir = "E:/temp/T226";
		String serverDir = "/upload";
		try {
//			FileUtils.copyFile(file, new File(realDir + "/" +fileFileName));
			copyFile(file, new File(realDir + "/" +fileFileName));
			clz.setPic(serverDir + "/" +fileFileName);
			this.clzDao.edit(clz);
		} catch (IOException | NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | SQLException e) {
			e.printStackTrace();
		}
		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();
	}

	public String add() {
		try {
			this.code = this.clzDao.add(clz);
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
				| SQLException e) {
			e.printStackTrace();
		}
		return "toList";
	}

	public String del() {
		try {
			this.clzDao.del(clz);
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
				| SQLException e) {
			e.printStackTrace();
		}
		return "toList";
	}

	public String edit() {
		try {
			this.clzDao.edit(clz);
		} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
				| SQLException e) {
			e.printStackTrace();
		}
		return "toList";
	}

	@Override
	public Clazz getModel() {
		return clz;
	}

}

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.hc.web.one.HelloAction" method="{1}">
	   <result name="success">/success.jsp</result>
	</action>
	
	<action name="/clz_*" class="com.hc.crud.web.ClazzAction" method="{1}">
	   <result name="list">/clzList.jsp</result>
	   <result name="preSave">/clzEdit.jsp</result>
	    <result name="preUpload">/clzUpload.jsp</result>
	   <result name="toList" type="redirectAction">clz_list</result>
	</action>
	</package>
</struts>

jsp界面
代码

</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="确定">
			<input type="hidden" name="rows" value="3">
	</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="height: 50px;width: 60px" src="${pageContext.request.contextPath}${b.pic }">
				</td>
				<td>**加粗样式**
					<a href="${pageContext.request.contextPath}/sy/clz_preSave.action?cid=${b.cid}">修改</a>&nbsp;
					<a href="${pageContext.request.contextPath}/sy/clz_del.action?cid=${b.cid}">删除</a>&nbsp;
					<a href="${pageContext.request.contextPath}/sy/clz_preUpload.action?cid=${b.cid}">图片上传</a>
				</td>
			</tr>
		</c:forEach>
	</table>
	<z:page pageBean="${pageBean }"></z:page>
</body>
</html>

图片上传界面
代码:

<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="${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">
</form>
</body>

存储图片的文件夹的路径
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值