struts的图片上传

今天我们要写一个图片上传功能我们要基于昨天的内容上写:有什么不知道的可以看一下我们昨天的博客:struts的增删查改

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

今天我们只讲第二种:上传到指定文件目录,添加服务器与真实目录的映射关系,从而解耦上传文件与tomcat的关系 , 文件服务器

我们要完成图片上传以及页面展示
在这里插入图片描述
接下来我们要来写一下实现上传图片的代码:
ClazzAction控制器
1、定义好文件的属性,文件类型,文件名
2、接收文件和选择保存文件的路径
3、修改数据库中的图片路径字段,改成服务器能读到的路径
4,我们还写一个可以利用缓冲流进行拷贝的

  //这里的属性名要和name对应    file是xxx
	private File file;
	//这里就xxxfileFileName
	private String fileFileName;
	//这里xxxfileContentype
	private String fileContentype;
	
	/**
	 * 跳转上传图片的界面
	 * @return
	 */
	public String preUpload() {
	
			try {
				this.result= this.clazzDao.list(clazz, null).get(0);
			} catch (InstantiationException | IllegalAccessException | SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return "preUpload";
		}
	
	
	
	public String upload() {
		String realDir="E:/T226";
		String severDir="/upload";
		try {
			//FileUtils.copyFile(file,new File(realDir +"/" +fileFileName));
			copyFile(file, new File(realDir +"/" +fileFileName));
			clazz.setPic(severDir +"/" +fileFileName);
			this.clazzDao.edit(clazz);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchFieldException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "toList";
	}
	
	/**
	 * 利用缓冲流进行拷贝
	 * @param sourec
	 * @param target
	 * @throws IOException 
	 */
	
	public void copyFile(File sourec,File target) throws IOException {
		BufferedInputStream in=new BufferedInputStream(new FileInputStream(sourec));
		BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream(target));
	    byte[] buf=new byte[1024];
	    int len=0;
	    while((len=in.read())!=-1) {
	    	out.write(buf, 0, len);
	    }
	    in.close();
	    out.close();
	}
	
	

然后我们创建一个页面: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">
</form>

</body>
</html>

然后我们还要在Tomcat的servers的server.xml加一条下面这一条代码就可以了

 <Context path="/T226_struts1/upload" docBase="E:/T226/"/>

方便大家知道 我们就截个图
在这里插入图片描述

然后我们在配置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.thf.web.HelloAction" method="{1}">
	<result name="success">/success.jsp</result>
	</action>
	
	<action name="/demo_*" class="com.thf.web.HelloAction" method="{1}">
	<result name="success">/success.jsp</result>
	</action>
	
	<action name="/clz_*" class="com.thf.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>

效果就是:
在这里插入图片描述
然后就上传到页面了
最后完整的效果就是这个:
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值