标签:
说到上传图片,最近在赶一个项目,也遇到了要上传头像的难题,在网上搜了好久,都没有找到满意的答案。于是又选择了最原始的上传图片的方法,在此做个记录,谨作备忘之用。
首先是添加页面add_UI.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<%@include file="/common/header.jsp"%>
<title>用户管理</title>
<script type="text/javascript" src="${basePath }js/datepicker/WdatePicker.js"></script>
</head>
<body class="rightBody">
<form id="form" name="form" action="${basePat }user/user_add.action" method="post" enctype="multipart/form-data">
<div class="p_d_1">
<div class="p_d_1_1">
<div class="content_info">
<div class="c_crumbs"><div><b></b><strong>用户管理</strong> - 新增用户</div></div>
<div class="tableH2">新增用户</div>
<table id="baseInfo" width="100%" align="center" class="list" border="0" cellpadding="0" cellspacing="0" >
<tr>
<td class="tdBg" width="200px">所属部门:</td>
<td><s:select name="user.dept" list="#{‘部门A‘:‘部门A‘,‘部门B‘:‘部门B‘ }"/></td>
</tr>
<tr>
<td class="tdBg" width="200px">头像:</td>
<td>
<input type="file" name="headImg"/>
</td>
</tr>
<tr>
<td class="tdBg" width="200px">用户名:</td>
<td><s:textfield name="user.name"/> </td>
</tr>
<tr>
<td class="tdBg" width="200px">帐号:</td>
<td><s:textfield name="user.account"/></td>
</tr>
<tr>
<td class="tdBg" width="200px">密码:</td>
<td><s:textfield name="user.password"/></td>
</tr>
</table>
<div class="tc mt20">
<input type="submit" class="btnB2" value="保存" />
<input type="button" onclick="javascript:history.go(-1)" class="btnB2" value="返回" />
</div>
</div></div></div>
</form>
</body>
</html>
然后是UserAction.java
import java.io.File; import java.util.List; import java.util.UUID; import javax.annotation.Resource; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletResponse; import org.apache.commons.io.FileUtils; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class UserAction extends BaseAction { @Resource private UserService userService; private User user; private File headImg; private String headImgContentType; private String headImgFileName; //保存新增 public String add(){ try { if(user != null){ //处理头像 if(headImg != null){ //1、保存头像,保存头像文件到服务器中 String filePath = ServletActionContext.getServletContext().getRealPath("/upload/user"); String fileName = UUID.randomUUID().toString().replaceAll("-", "") + headImgFileName.substring(headImgFileName.lastIndexOf(".")); FileUtils.copyFile(headImg, new File(filePath, fileName)); //2、设置到用户头像 user.setHeadImg("user/"+fileName); } //保存用户及其角色 userService.saveUser(user); } } catch (Exception e) { e.printStackTrace(); } return "list"; } public User getUser() { return user; } public void setUser(User user) { this.user = user; } public File getHeadImg() { return headImg; } public void setHeadImg(File headImg) { this.headImg = headImg; } public String getHeadImgContentType() { return headImgContentType; } public void setHeadImgContentType(String headImgContentType) { this.headImgContentType = headImgContentType; } public String getHeadImgFileName() { return headImgFileName; } public void setHeadImgFileName(String headImgFileName) { this.headImgFileName = headImgFileName; } }
注意:
headImg,headImgContentType,headImgFileName这三个变量的名称不要改,因为改了可能不成功。
至于具体的saveUser(user)方法就是保存用户的方法啦,这里就不赘述了。
而其他ssh的配置文件和其他java待有空的时候再总结个demo出来吧。
标签:
原文地址:http://www.cnblogs.com/yeqrblog/p/4398914.html