說到上傳圖片,最近在趕一個項目,也遇到了要上傳頭像的難題,在網上搜了好久,都沒有找到滿意的答案。於是又選擇了最原始的上傳圖片的方法,在此做個記錄,謹作備忘之用。
首先是添加頁面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出來吧。
