首页 > 代码库 > struts 头像上传

struts 头像上传

java代码:

1
package cn.itcast.nsfw.user.action; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.util.List; 6 import java.util.UUID; 7 8 import javax.annotation.Resource; 9 10 import org.apache.commons.io.FileUtils; 11 import org.apache.struts2.ServletActionContext; 12 13 import cn.itcast.nsfw.user.entity.User; 14 import cn.itcast.nsfw.user.service.UserService; 15 16 import com.opensymphony.xwork2.ActionSupport; 17 18 public class UserAction extends ActionSupport { 19 20 @Resource 21 private UserService userService; 22 private List<User> userList; 23 private User user; 24 private String[] selectedRow; 25 private File headImg; 26 private String headImgContentType; 27 private String headImgFileName; 28 29 //列表页面 30 public String listUI(){ 31 userList = userService.findObjects(); 32 return "listUI"; 33 } 34 //跳转到新增页面 35 public String addUI(){ 36 return "addUI"; 37 } 38 //保存新增 39 public String add(){ 40 try { 41 if(user != null){ 42 //处理头像 43 if(headImg != null){ 44 //1、保存头像到upload/user 45 //获取保存路径的绝对地址 46 String filePath = ServletActionContext.getServletContext().getRealPath("upload/user"); 47 String fileName = UUID.randomUUID().toString().replaceAll("-", "") + headImgFileName.substring(headImgFileName.lastIndexOf(".")); 48 //复制文件 49 FileUtils.copyFile(headImg, new File(filePath, fileName)); 50 51 //2、设置用户头像路径 52 user.setHeadImg("user/" + fileName); 53 } 54 userService.save(user); 55 } 56 } catch (Exception e) { 57 e.printStackTrace(); 58 } 59 return "list"; 60 } 61 //跳转到编辑页面 62 public String editUI(){ 63 if (user != null && user.getId() != null) { 64 user = userService.findObjectById(user.getId()); 65 } 66 return "editUI"; 67 } 68 //保存编辑 69 public String edit(){ 70 try { 71 if(user != null){ 72 //处理头像 73 if(headImg != null){ 74 //1、保存头像到upload/user 75 //获取保存路径的绝对地址 76 String filePath = ServletActionContext.getServletContext().getRealPath("upload/user"); 77 String fileName = UUID.randomUUID().toString().replaceAll("-", "") + headImgFileName.substring(headImgFileName.lastIndexOf(".")); 78 //复制文件 79 FileUtils.copyFile(headImg, new File(filePath, fileName)); 80 81 //2、设置用户头像路径 82 user.setHeadImg("user/" + fileName); 83 } 84 85 userService.update(user); 86 } 87 } catch (Exception e) { 88 e.printStackTrace(); 89 } 90 return "list"; 91 } 92 //删除 93 public String delete(){ 94 if(user != null && user.getId() != null){ 95 userService.delete(user.getId()); 96 } 97 return "list"; 98 } 99 //批量删除 100 public String deleteSelected(){ 101 if(selectedRow != null){ 102 for(String id: selectedRow){ 103 userService.delete(id); 104 } 105 } 106 return "list"; 107 } 108 public List<User> getUserList() { 109 return userList; 110 } 111 public void setUserList(List<User> userList) { 112 this.userList = userList; 113 } 114 public User getUser() { 115 return user; 116 } 117 public void setUser(User user) { 118 this.user = user; 119 } 120 public String[] getSelectedRow() { 121 return selectedRow; 122 } 123 public void setSelectedRow(String[] selectedRow) { 124 this.selectedRow = selectedRow; 125 } 126 public File getHeadImg() { 127 return headImg; 128 } 129 public void setHeadImg(File headImg) { 130 this.headImg = headImg; 131 } 132 public String getHeadImgContentType() { 133 return headImgContentType; 134 } 135 public void setHeadImgContentType(String headImgContentType) { 136 this.headImgContentType = headImgContentType; 137 } 138 public String getHeadImgFileName() { 139 return headImgFileName; 140 } 141 public void setHeadImgFileName(String headImgFileName) { 142 this.headImgFileName = headImgFileName; 143 } 144 145 }

头像上传jsp页面(采用struts标签)

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <html>
 3 <head>
 4     <%@include file="/common/header.jsp"%>
 5     <title>用户管理</title>
 6     <script type="text/javascript" src="http://www.mamicode.com/${basePath }js/datepicker/WdatePicker.js"></script>
 7 </head>
 8 <body class="rightBody">
 9 <form id="form" name="form" action="${basePath }nsfw/user_add.action" method="post" enctype="multipart/form-data">
10     <div class="p_d_1">
11         <div class="p_d_1_1">
12             <div class="content_info">
13     <div class="c_crumbs"><div><b></b><strong>用户管理</strong>&nbsp;-&nbsp;新增用户</div></div>
14     <div class="tableH2">新增用户</div>
15     <table id="baseInfo" width="100%" align="center" class="list" border="0" cellpadding="0" cellspacing="0"  >
16         <tr>
17             <td class="tdBg" width="200px">所属部门:</td>
18             <td><s:select name="user.dept" list="#{‘部门A‘:‘部门A‘,‘部门B‘:‘部门B‘ }"/></td>
19         </tr>
20         <tr>
21             <td class="tdBg" width="200px">头像:</td>
22             <td>
23                 <input type="file" name="headImg"/>
24             </td>
25         </tr>
26         <tr>
27             <td class="tdBg" width="200px">用户名:</td>
28             <td><s:textfield name="user.name"/> </td>
29         </tr>
30         <tr>
31             <td class="tdBg" width="200px">帐号:</td>
32             <td><s:textfield name="user.account"/></td>
33         </tr>
34         <tr>
35             <td class="tdBg" width="200px">密码:</td>
36             <td><s:textfield name="user.password"/></td>
37         </tr>
38         <tr>
39             <td class="tdBg" width="200px">性别:</td>
40             <td><s:radio list="#{‘true‘:‘男‘,‘false‘:‘女‘}" name="user.gender"/></td>
41         </tr>
42         <tr>
43             <td class="tdBg" width="200px">角色:</td>
44             <td></td>
45         </tr>
46         <tr>
47             <td class="tdBg" width="200px">电子邮箱:</td>
48             <td><s:textfield name="user.email"/></td>
49         </tr>
50         <tr>
51             <td class="tdBg" width="200px">手机号:</td>
52             <td><s:textfield name="user.mobile"/></td>
53         </tr>
54         <tr>
55             <td class="tdBg" width="200px">生日:</td>
56             <td><s:textfield id="birthday" name="user.birthday" readonly="true" onfocus="WdatePicker({‘skin‘:‘whyGreen‘,‘dateFmt‘:‘yyyy-MM-dd‘});" /></td>
57         </tr>
58         <tr>
59             <td class="tdBg" width="200px">状态:</td>
60             <td><s:radio list="#{‘1‘:‘有效‘,‘0‘:‘无效‘}" name="user.state" value="http://www.mamicode.com/1"/></td>
61         </tr>
62         <tr>
63             <td class="tdBg" width="200px">备注:</td>
64             <td><s:textarea name="user.memo" cols="75" rows="3"/></td>
65         </tr>
66     </table>
67     <div class="tc mt20">
68         <input type="submit" class="btnB2" value="http://www.mamicode.com/保存" />
69         &nbsp;&nbsp;&nbsp;&nbsp;
70         <input type="button"  onclick="javascript:history.go(-1)" class="btnB2" value="http://www.mamicode.com/返回" />
71     </div>
72     </div></div></div>
73 </form>
74 </body>
75 </html>

头像编辑页面:

 1 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 2 <html>
 3 <head>
 4     <%@include file="/common/header.jsp"%>
 5     <title>用户管理</title>
 6     <script type="text/javascript" src="http://www.mamicode.com/${basePath }js/datepicker/WdatePicker.js"></script>
 7 </head>
 8 <body class="rightBody">
 9 <form id="form" name="form" action="${basePath }nsfw/user_edit.action" method="post" enctype="multipart/form-data">
10     <div class="p_d_1">
11         <div class="p_d_1_1">
12             <div class="content_info">
13     <div class="c_crumbs"><div><b></b><strong>用户管理</strong>&nbsp;-&nbsp;编辑用户</div></div>
14     <div class="tableH2">编辑用户</div>
15     <table id="baseInfo" width="100%" align="center" class="list" border="0" cellpadding="0" cellspacing="0"  >
16         <tr>
17             <td class="tdBg" width="200px">所属部门:</td>
18             <td><s:select name="user.dept" list="#{‘部门A‘:‘部门A‘,‘部门B‘:‘部门B‘ }"/></td>
19         </tr>
20         <tr>
21             <td class="tdBg" width="200px">头像:</td>
22             <td>
23                 <s:if test="%{user.headImg != null && user.headImg != ‘‘}">
24                     <img src="http://www.mamicode.com/${basePath }upload/" width="100" height="100"/>
25                     <s:hidden name="user.headImg"/>
26                 </s:if>
27                 <input type="file" name="headImg"/>
28             </td>
29         </tr>
30         <tr>
31             <td class="tdBg" width="200px">用户名:</td>
32             <td><s:textfield name="user.name"/> </td>
33         </tr>
34         <tr>
35             <td class="tdBg" width="200px">帐号:</td>
36             <td><s:textfield name="user.account"/></td>
37         </tr>
38         <tr>
39             <td class="tdBg" width="200px">密码:</td>
40             <td><s:textfield name="user.password"/></td>
41         </tr>
42         <tr>
43             <td class="tdBg" width="200px">性别:</td>
44             <td><s:radio list="#{‘true‘:‘男‘,‘false‘:‘女‘}" name="user.gender"/></td>
45         </tr>
46         <tr>
47             <td class="tdBg" width="200px">角色:</td>
48             <td></td>
49         </tr>
50         <tr>
51             <td class="tdBg" width="200px">电子邮箱:</td>
52             <td><s:textfield name="user.email"/></td>
53         </tr>
54         <tr>
55             <td class="tdBg" width="200px">手机号:</td>
56             <td><s:textfield name="user.mobile"/></td>
57         </tr>        
58         <tr>
59             <td class="tdBg" width="200px">生日:</td>
60             <td>
61             <s:textfield id="birthday" name="user.birthday" readonly="true" 
62             onfocus="WdatePicker({‘skin‘:‘whyGreen‘,‘dateFmt‘:‘yyyy-MM-dd‘});" >
63                 <s:param name="value"><s:date name="user.birthday" format="yyyy-MM-dd"/></s:param>
64             </s:textfield>
65             </td>
66         </tr>
67         <tr>
68             <td class="tdBg" width="200px">状态:</td>
69             <td><s:radio list="#{‘1‘:‘有效‘,‘0‘:‘无效‘}" name="user.state"/></td>
70         </tr>
71         <tr>
72             <td class="tdBg" width="200px">备注:</td>
73             <td><s:textarea name="user.memo" cols="75" rows="3"/></td>
74         </tr>
75     </table>
76     <s:hidden name="user.id"/>
77     <div class="tc mt20">
78         <input type="submit" class="btnB2" value="http://www.mamicode.com/保存" />
79         &nbsp;&nbsp;&nbsp;&nbsp;
80         <input type="button"  onclick="javascript:history.go(-1)" class="btnB2" value="http://www.mamicode.com/返回" />
81     </div>
82     </div></div></div>
83 </form>
84 </body>
85 </html>

 

struts 头像上传