Struts之文件上传

导读:本篇文章讲解 Struts之文件上传,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

文件上传( 三种上传方案):
1、上传到tomcat服务器 ;

      ①自己的电脑,项目在哪里,图片就在哪里;

      ②云服务器:是没有CDEF盘的,只有/根目录

 2(用的多)、上传到指定文件目录,添加服务器与真实目录的映射关系,从而解耦上传文件与tomcat的关系文件服务器和web服务器通常是一个,但是文件目录与Tomcat目录肯定不是同一个

3、在数据库表中建立二进制字段,将图片存储到数据库;

      安全性比第二种高

服务器:ECS云服务器

struts上传的注意:
1、上传文件界面:enctype=”multipart/form-data”    type=”file”

2、struts必须按照指定的格式去接收参数变量

开发:

         upload.jsp

         web层:

                   跳转方法–>upload.jsp

                  上传的方法:①将指定的文件图片上传到文件目录;

                                       ②将指定图片的请求映射地址更新到数据

一、使用第二种文件上传

①.创建upload.jsp

注意:要记得定义多功能表单    enype=”multiput/form-data”

<%@ 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="img" />
		<input type="submit" />
	</form>
</body>
</html>

.在ClzAction 增加两个方法

package com.zking.crud.web;

import java.io.File;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.zking.crud.dao.ClzDao;
import com.zking.crud.entity.Clz;
import com.zking.crud.util.BaseAction;
import com.zking.crud.util.PageBean;

public class ClzAction extends BaseAction<Clz>{
	private Clz clz=new Clz();
	private ClzDao clzDao=new ClzDao();
	
	private File img;
	private String imgFileName;
	private String imgContentType;

	public File getImg() {
		return img;
	}

	public void setImg(File img) {
		this.img = img;
	}

	public String getImgFileName() {
		return imgFileName;
	}

	public void setImgFileName(String imgFileName) {
		this.imgFileName = imgFileName;
	}

	public String getImgContentType() {
		return imgContentType;
	}

	public void setImgContentType(String imgContentType) {
		this.imgContentType = imgContentType;
	}
	/**
	 * 查询班级列表
	 * @return
	 * @throws Exception
	 */
	public String list() throws Exception {
		PageBean pageBean=new PageBean();
		pageBean.setRequest(req);
		this.result=this.clzDao.list(clz, pageBean);
		this.req.setAttribute("result", result);
		this.req.setAttribute("pageBean", pageBean);
		return LIST;
	}
	
	/**
	 * 跳转新增或者修改界面
	 * @return
	 * @throws Exception
	 */
	public String toEdit() throws Exception {
		int cid=clz.getCid();
		if(cid!=0) {
			this.result=this.clzDao.list(clz, null).get(0);
			this.req.setAttribute("result",result);
		}
		return TOEDIT;
	}
	/**
	 * 往数据库新增数据
	 * @return
	 * @throws Exception
	 */
	public String add() throws Exception {
		this.clzDao.add(clz);
		return TOLIST;
	}
	/**
	 * 修改数据
	 * @return
	 * @throws Exception
	 */
	public String edit() throws Exception {
		this.clzDao.edit(clz);
		return TOLIST;
	}
	/**
	 * 删除数据
	 * @return
	 * @throws Exception
	 */
	public String del() throws Exception {
		this.clzDao.del(clz);
		return TOLIST;
	}
	
	
	/**
	 * 按照指定的格式去接受参数变量
	 * 1.上传的文件
	 * 2.上传的文件名
	 * 3.上传的文件类别
	 */
	
	
	/**
	 * 跳转到文件上传界面
	 * @return
	 * @throws Exception
	 */
	public String preUpload() throws Exception {
		this.result=this.clzDao.list(clz,null).get(0);
		this.req.setAttribute("result", result);
		return "upload";
	}
	
	public String upload() throws Exception {
		//img代表客户选择的文件或者图片,接下来要将图片上传到其他地方
		//img代表了源头,要将其写入目的地target
		String destDir="F:/y1/img";
		String serverDir="/uploadImages";
		//F:/y1/img/imgFileName
		FileUtils.copyFile(img, new File(destDir+"/"+imgFileName));
		//数据库保存的值是/upload/xx.png
		//图片是在F:/y1/img
		//访问:http://localhost:8080/struts/uploadImages/xx.png
		clz.setPic(serverDir+"/"+imgFileName);
		this.clzDao.edit(clz);
		return TOLIST;
	}
	
	@Override
	public Clz getModel() {
		return clz;
	}
	
}

③.struts-sy.xml配置

<result name=”upload”>/upload.jsp</result>

④.clzList.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://jsp.veryedu.cn" 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">
<link
	href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/css/bootstrap.css"
	rel="stylesheet">
<script
	src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/4.5.0/js/bootstrap.js"></script>
<title>工作列表</title>
<style type="text/css">
.page-item input {
	padding: 0;
	width: 40px;
	height: 100%;
	text-align: center;
	margin: 0 6px;
}

.page-item input, .page-item b {
	line-height: 38px;
	float: left;
	font-weight: 400;
}

.page-item.go-input {
	margin: 0 10px;
}
</style>
</head>
<body>
	<form class="form-inline"
		action="${pageContext.request.contextPath }/sy/clz_list.action" method="post">
		<div class="form-group mb-2">
			<input type="text" class="form-control-plaintext" name="job_title"
				placeholder="请输入标题名称">
<!-- 			<input name="rows" value="20" type="hidden"> -->
<!-- 不想分页 -->
				<input name="pagination" value="false" type="hidden">
		</div>
		<button type="submit" class="btn btn-primary mb-2">查询</button>
		<a class="btn btn-primary mb-2" href="${pageContext.request.contextPath }/sy/clz_toEdit.action">新增</a>
	</form>

	<table class="table table-striped bg-success">
		<thead>
			<tr>
				<th scope="col">ID</th>
				<th scope="col">班级名字</th>
				<th scope="col">教员</th>
				<th scope="col">图片</th>
				<th scope="col">操作</th>
			</tr>
		</thead>
		<tbody>
			<c:forEach  var="b" items="${result}">
			<tr>
				<td>${b.cid }</td>
				<td>${b.cname }</td>
				<td>${b.cteacher }</td>
				<td>
				<img alt="" src="${b.pic }" style="width:50px;height:70px;">
				</td>
				<td>
					<a href="${pageContext.request.contextPath }/sy/clz_toEdit.action?cid=${b.cid}">修改</a>
					<a href="${pageContext.request.contextPath }/sy/clz_del.action?cid=${b.cid}">删除</a>
					<a href="${pageContext.request.contextPath }/sy/clz_preUpload.action?cid=${b.cid}">上传图片</a>
				</td>
			</tr>
			</c:forEach>
		</tbody>
	</table>
	<!-- 这一行代码就相当于前面分页需求前端的几十行了 -->
	<z:page pageBean="${pageBean }"></z:page>

</body>
</html>

⑤.server.xml中增加

<Context docBase=”F:/y1/img” path=”/uploadImages”/>

⑥.结果展示

Struts之文件上传

 

Struts之文件上传

 Struts之文件上传

byebye~~~

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之家整理,本文链接:https://www.bmabk.com/index.php/post/12230.html

(0)
小半的头像小半

相关推荐

极客之家——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!