[img]http://dl.iteye.com/upload/picture/pic/110517/ec6ae192-500d-3ecb-81be-6c22c3319b37.jpg[/img]
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";
}
}
scanActivity
/view/error.jsp
/view/error.jsp
/view/admin/activity/createActivity.jsp
/view/globalError.jsp
/uploadImages
<%@ 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 + "/";
%>
My JSP 'createNews.jsp' starting page
<%!String sourceURL = "http://192.168.26.195:8080/btw";%>
href="<%=sourceURL%>/ckeditor/_samples/sample.css" />
href="<%=sourceURL%>/css/body.css" />
href="<%=sourceURL%>/css/createActivity.css" />
src="<%=sourceURL%>/js/My97DatePicker/WdatePicker.js">
basePath="http://192.168.26.195:8080/btw/ckeditor/" />