package cn.itcast.usermng.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import cn.itcast.commons.CommonUtils;
import cn.itcast.usermng.domain.User;
import cn.itcast.usermng.service.UserService;
import cn.itcast.usermng.utils.DownUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;
public class UserAction extends ActionSupport implements ModelDriven<User>{
private UserService userService = new UserService();
private User model = new User();
//上传
private File resume;
private String resumeFileName;
//一个流两个头
private InputStream inputStream;
private String type;
private String frameName;
public InputStream getInputStream() {
return inputStream;
}
public String getType() {
return type;
}
public String getFrameName() {
return frameName;
}
public void setResume(File resume) {
this.resume = resume;
}
/**
* 处理文件名称是绝对路径的问题
* @param resumeFileName
*/
public void setResumeFileName(String resumeFileName) {
int index = resumeFileName.lastIndexOf("\\");
if(index != -1){
this.resumeFileName = resumeFileName.substring(index+1);
}else{
this.resumeFileName = resumeFileName;
}
}
@Override
public User getModel() {
return model;
}
public void setModel(User model) {
this.model = model;
}
/**
* 用户登录
* @return
*/
@InputConfig(resultName="login")
public String login(){
User user = userService.login(model.getLoginname(),model.getLoginpass());
if(user == null){
this.addActionError("用户名或密码错误");
return LOGIN;
}
ServletActionContext.getRequest().getSession().setAttribute("user",user);
return SUCCESS;
}
/***
* 查询所有
* @return
*/
public String findAll(){
ActionContext.getContext().getValueStack().push(userService.findAll());
return "list";
}
/***
* 组合查询
* @return
*/
public String findByCombination(){
ActionContext.getContext().getValueStack().push(userService.findByCombination(model));
return "list";
}
/**
* 添加用户
* @return
*/
public String add(){
//1. 给model添加uid
model.setUid(CommonUtils.uuid());
//2. 保存上传的文件(并把文件名称赋给model的filename和filepath)
if(resume!=null){
saveResume(model);
}else{
model.setFilename(null);
model.setFilepath(null);
}
//3. 调用userService的方法完成向数据库插入记录
userService.add(model);
//4、重定向到findAll
return "redirectFindAll";
}
/**
* 查看用户资料
* @return
*/
public String findByUid(){
ActionContext.getContext().getValueStack().push(userService.load(model.getUid()));
return "view";
}
/**
* 在编辑页面中显示用户信息
* @return
*/
public String editPre(){
ActionContext.getContext().getValueStack().push(userService.load(model.getUid()));
return "edit";
}
public String editUser(){
if(resume !=null){ //修改了简历,要删除老简历,增加新简历
deleteResume(model);
saveResume(model);
}
//原来的简历信息已经通过隐藏字段传给model了
userService.editUser(model);
return "redirectFindAll";
}
/**
* 删除简历
* @param model2
*/
private void deleteResume(User model) {
/*得到File才能删除
* > 得到路径
* > 得到真实名称*/
String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/resumes");
File destfile = new File(path,model.getFilepath());
if(destfile.exists()){
destfile.delete();
}
}
/**
* 保存简历
* @param model2
*/
private void saveResume(User model) {
/* 保存文件:
* 1. 有源文件: resume
* 2. 有目标文件:获取保存路径(/WEB-INF/resumes),文件名称(真实名称,即带有uuid前缀的名称)
* 3. FileUtils.copyFile(src, dest);
*/
//获取保存路径
String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/resumes");
//获取文件真实名称
String realname = CommonUtils.uuid() + this.resumeFileName;
//目标文件
File destFile = new File(path,realname);
//保存文件
try {
FileUtils.copyFile(resume, destFile);
} catch (IOException e) {
e.printStackTrace();
}
//给model的filename和filepath赋值
model.setFilename(resumeFileName);
model.setFilepath(realname);
}
/**
* 下载简历
* @return
*/
public String download(){
//通过超链接带过去的参数已经到了model中
try{
String filename = new String(model.getFilename().getBytes("ISO-8859-1"),"utf-8");
String filepath = new String(model.getFilepath().getBytes("ISO-8859-1"),"utf-8");
//找到磁盘上的文件
String path = ServletActionContext.getServletContext().getRealPath("/WEB-INF/resumes");
File file = new File(path,filepath);
//得到流,得到mime类型,得到框框中显示的名称
inputStream = new FileInputStream(file);
type = ServletActionContext.getServletContext().getMimeType(file.getAbsolutePath());
String agent = ServletActionContext.getRequest().getHeader("User-Agent");
frameName = DownUtils.encodeDownloadFilename(filename, agent);
return "download";
}catch(Exception e){
e.printStackTrace();
return null;
}
}
/**
* 删除用户资料,需要从数据库中删除该条记录,并删除简历
*/
public String delete(){
User user = userService.load(model.getUid());
if(user.getFilename()!=null){
deleteResume(user);
}
userService.delete(model.getUid());
return "redirectFindAll";
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
struts2+DBUtils实现的用户信息增删改查,包含文件上传下载
共177个文件
gif:79个
jar:19个
jpg:17个
4星 · 超过85%的资源 需积分: 10 54 下载量 132 浏览量
2016-04-07
15:17:31
上传
评论 3
收藏 7.34MB RAR 举报
温馨提示
1、一个完美struts2写成的小项目,实现了用户信息的增删改查。 2、采用的是分层结构,dao,service,action三层架构,结构清晰 3、完整的写出了文件上传,文件下载的具体操作。
资源推荐
资源详情
资源评论
收起资源包目录
struts2+DBUtils实现的用户信息增删改查,包含文件上传下载 (177个子文件)
time.bmp 1KB
UserAction.class 6KB
UserDao.class 5KB
User.class 3KB
UserService.class 2KB
DownUtils.class 1KB
JdbcUtils.class 742B
.classpath 529B
org.eclipse.wst.common.component 459B
org.eclipse.wst.jsdt.ui.superType.container 49B
Style.css 13KB
jquery.datepick.css 4KB
dtree.css 795B
left.css 220B
fielderror.ftl 2KB
fielderror.ftl 2KB
actionmessage.ftl 1KB
actionmessage.ftl 1KB
actionerror.ftl 1KB
actionerror.ftl 1KB
logintitle.gif 16KB
loginbg.gif 5KB
aaa.gif 2KB
globe.gif 1KB
trash.gif 1KB
question.gif 1KB
base.gif 1KB
optionbgOn.gif 964B
cart.gif 955B
calendar-green.gif 919B
nolines_plus.gif 870B
nolines_minus.gif 861B
index_09.gif 841B
button_phone01.gif 758B
about.gif 710B
button_view.gif 669B
button_search.gif 664B
button_modi.gif 649B
button_print.gif 634B
musicfolder.gif 633B
imgfolder.gif 622B
icon_upfolder.gif 603B
button_exit.gif 590B
page.gif 582B
button_cancel.gif 570B
button_ok.gif 570B
button_help.gif 564B
mis_15.gif 403B
top.gif 376B
folderopen.gif 376B
button_del.gif 375B
button_add.gif 373B
folder.gif 372B
button_save.gif 366B
button_alert.gif 332B
icon-pencil-x.gif 320B
button_phone.gif 309B
icon-pencil.gif 308B
calendar.gif 269B
dot.gif 253B
button_close01.gif 250B
cd.gif 239B
000.gif 237B
zoom.gif 224B
icon_zoom.gif 224B
button_home.gif 220B
optionbgOff.gif 218B
icon_time.gif 216B
mis_12.gif 201B
bt_icon_export.gif 195B
button_search01.gif 192B
modernliving.gif 163B
button_clock.gif 146B
icon-calendar.gif 139B
i_edit.gif 132B
i_ok.gif 130B
mis_13.gif 130B
i_del.gif 125B
hot2.gif 114B
bg.gif 109B
button_bg.gif 93B
icon-delete.gif 91B
plus.gif 89B
plusbottom.gif 88B
minus.gif 86B
prev.gif 85B
minusbottom.gif 85B
2spcr2x158.gif 74B
join.gif 69B
line.gif 66B
joinbottom.gif 66B
mis_002.gif 64B
empty.gif 62B
add.gif 58B
open.gif 57B
mis_003.gif 54B
mis_03.gif 49B
ad_03.gif 45B
shim.gif 43B
mysql-connector-java-5.1.13-bin.jar 1.36MB
共 177 条
- 1
- 2
资源评论
- 勇豪2016-12-22初学者学习使用
jing_php
- 粉丝: 1
- 资源: 3
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功