简单的上传文件Ⅱ


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>

猜你喜欢

转载自xiongjiajia.iteye.com/blog/1465952