`

简单的上传文件Ⅱ

 
阅读更多

package com.baitw.struts.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Upload {

	public String uploadImage(File upload,String savePath,String uploadFileName) throws IOException{
		
		FileOutputStream fos=new FileOutputStream(savePath+"\\"+uploadFileName);
		FileInputStream fis=new FileInputStream(upload);
		byte[] buffer=new byte[1024];
		int length=0;
		while((length=fis.read(buffer))>0){
			fos.write(buffer,0,length);
		}
		return "upload_success";
	}
}

package com.baitw.struts.action;



import java.io.File;
import java.util.Date;

import org.apache.struts2.ServletActionContext;

import com.baitw.dao.ActivityDao;
import com.baitw.dao.CardTypeDao;
import com.baitw.dao.UserDao;
import com.baitw.hibernate.entity.TActivity;
import com.baitw.struts.utils.Upload;
import com.opensymphony.xwork2.ActionSupport;

/**
 * 
 * 添加活动
 * 
 * */

public class AddActivity extends ActionSupport {

	
	private String title;
	private Date begintime;
	private Date endtime;
	private String content;
	private Boolean state;
	private Date createtime;
	private Long cardTypeid;
	private String username;
	private ActivityDao activityDao;
	private CardTypeDao  cardTypeDao;
	private UserDao userDao;
	/*-----------------------------------------------*/
	private File upload;
	private String uploadContextType;
	private String uploadFileName;
	private String savePath;

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContextType() {
		return uploadContextType;
	}

	public void setUploadContextType(String uploadContextType) {
		this.uploadContextType = uploadContextType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String getSavePath() {
		return ServletActionContext.getRequest().getRealPath(savePath);
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}
	/*-----------------------------------------------*/

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}

	public void setCardTypeDao(CardTypeDao cardTypeDao) {
		this.cardTypeDao = cardTypeDao;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public Date getBegintime() {
		return begintime;
	}

	public void setBegintime(Date begintime) {
		this.begintime = begintime;
	}

	public Date getEndtime() {
		return endtime;
	}

	public void setEndtime(Date endtime) {
		this.endtime = endtime;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public Boolean getState() {
		return state;
	}

	public void setState(Boolean state) {
		this.state = state;
	}

	public Date getCreatetime() {
		return createtime;
	}

	public void setCreatetime(Date createtime) {
		this.createtime = createtime;
	}

	public Long getCardTypeid() {
		return cardTypeid;
	}

	public void setCardTypeid(Long cardTypeid) {
		this.cardTypeid = cardTypeid;
	}

	public ActivityDao getActivityDao() {
		return activityDao;
	}

	public void setActivityDao(ActivityDao activityDao) {
		this.activityDao = activityDao;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		
		TActivity activity=new TActivity();
		activity.setTitle(title);
		activity.setTCardType(cardTypeDao.getTCardType(cardTypeid.toString()));
		activity.setCreatetime(createtime);
		activity.setBegintime(begintime);
		activity.setEndtime(endtime);
		activity.setContent(content);
		activity.setLogo(savePath+"/"+uploadFileName);
		activity.setState(true);
		activity.setTUser(userDao.getUserEntity(username));
		
		Upload upFile=new Upload();
		upFile.uploadImage(upload, ServletActionContext.getRequest().getRealPath(savePath), uploadFileName);
		
		return activityDao.addActivity(activity);
	}

	public String loadAddPage() throws Exception{
		
		return "loadAddPage";
	}
}

<!--活动添加 -->
		<action name="addActivity" class="addActivity">
			<result name="success" type="chain">scanActivity</result>
			<result name="error">/view/error.jsp</result>
			<result name="null">/view/error.jsp</result>
			<result name="loadAddPage">/view/admin/activity/createActivity.jsp</result>
			<result name="input">/view/globalError.jsp</result>
			<param name="savePath">/uploadImages</param>
		</action>

<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ taglib uri="http://ckeditor.com" prefix="ckeditor"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
	<head>
		<base href="<%=basePath%>">

		<title>My JSP 'createNews.jsp' starting page</title>

		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="cache-control" content="no-cache">
		<meta http-equiv="expires" content="0">
		<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
		<meta http-equiv="description" content="This is my page">
		<!--
		<link rel="stylesheet" type="text/css" href="styles.css">
		-->
		<%!String sourceURL = "http://192.168.26.195:8080/btw";%>
		<link type="text/css" rel="stylesheet"
			href="<%=sourceURL%>/ckeditor/_samples/sample.css" />
		<link type="text/css" rel="stylesheet"
			href="<%=sourceURL%>/css/body.css" />
		<link type="text/css" rel="stylesheet"
			href="<%=sourceURL%>/css/createActivity.css" />
		<script language="javascript" type="text/javascript"
			src="<%=sourceURL%>/js/My97DatePicker/WdatePicker.js"></script>
		<script type="text/javascript">
			function submit1(){
				targetForm=document.forms[0];
				targetForm.action="admin/addActivity.action";
			}
			function submit2(){
				targetForm=document.forms[0];
				targetForm.action="uploadImage.action";
			}
		</script>
	</head>
	<body>
		<div id="maincontent">
			<form action="" method="post" enctype="multipart/form-data">
				<div id="div_detailInfo">
					<table width="" border="0">
						<tr>
							<td width="70">
								<span class="tableChars">活动标题</span>
							</td>
							<td width="200">
								<label for="textfield"></label>
								<input type="text" name="title" id="title" class="text" />
							</td>
							<td width="70">
								<span class="tableChars">添加人</span>
							</td>
							<td width="200">
								<label for="textfield2"></label>
								<input type="text" name="username" id="username"
									class="text" value="${sessionScope.username}"
									readonly="readonly" />
							</td>
						</tr>
						<tr>
							<td>
								<span class="tableChars">开始时间</span>
							</td>
							<td>
								<input class="Wdate" type="text" onClick="WdatePicker()" name="begintime">
							</td>
							<td>
								<span class="tableChars">结束时间</span>
							</td>
							<td>
								<input class="Wdate" type="text" onClick="WdatePicker()" name="endtime">
							</td>
						</tr>
						<tr>
							<td>
								<span class="tableChars">发布时间</span>
							</td>
							<td>
								<input class="Wdate" type="text" onClick="WdatePicker()" name="createtime">
							</td>
							<td>
								<span class="tableChars">卡类型</span>
							</td>
							<td>
								<label for="select"></label>
								<select name="cardTypeid" id="cardTypeid" class="text">
									<option value="1">
										朕卡
									</option>
									<option value="2">
										OK卡
									</option>
									<option value="3">
										朕卡、OK卡
									</option>
								</select>
							</td>
						</tr>
						<tr>
							<td>
								<span class="tableChars">活动图片</span>
							</td>
							<td colspan="3">
								<input type="file" class="upload" name="upload"/>
							</td>
						</tr>
					</table>
				</div>
				<div id="div_editor" style="left: 10px; width: 894px;">
					<textarea cols="80" id="editor" name="content" rows="10"></textarea>
				</div>
				<div id="div_submit">
					<input type="submit" value="添加" onclick="submit1();"/>
				</div>
			</form>
		</div>
	</body>
	<ckeditor:replace replace="editor"
		basePath="http://192.168.26.195:8080/btw/ckeditor/" />
</html>
分享到:
评论

相关推荐

    碧彩条码打印秤BCⅡ800使用手册(管理员).doc

    软件方面,主要使用BMS2(碧彩管理系统2)进行商品资料的管理,包括导入、编辑和上传下载;BLD 5.23用于自定义标签设计和下传;UpdateProgBCII则用于秤的固件更新,确保设备软件始终保持最新状态。 BMS2软件的安装...

    Game Resource

    从提供的压缩包子文件的文件名称来看,我们有两个魔兽争霸III(Warcraft III)的地图文件:仙之侠道Ⅱ叁章.w3x和仙道苍云传测试3.w3x。魔兽争霸III是一款著名的即时战略游戏,允许玩家自定义地图进行对战或角色扮演...

    基于DSP的地下微水检测系统

    将采集的信号经过简单处理后上传到TMS320F2812进行分析处理,得到井下水面高度变化以及井下水温等参数的精确值,并送至液晶显示屏实时显示,同时对USB设备进行简单文件操作,便于数据存取。将μC/OS-Ⅱ实时操作系统...

    网络流量显示小工具

    通过直观的图表或者数字,用户可以随时看到上传和下载的数据量,了解当前网络活动的状态。这对于控制带宽使用,避免因大文件下载或视频流媒体服务而造成的网络拥堵至关重要。例如,在网络条件有限的环境下,用户可以...

Global site tag (gtag.js) - Google Analytics