使用struts2標籤上傳使用者頭像(一)
這些天用到在SSH框架下上傳使用者照片,原來以為挺複雜的事情,結果使用struts2預設的檔案上傳元件很輕鬆地就完成了。這是基礎的程式碼,隨後會在此基礎上改進。
1.基礎準備
使用struts2預設的檔案上傳元件:Common-FileUpload,需要在web應用中增加兩個JAR檔案,即common-io-*.jar和common-fileupload-*.jar,將Struts2專案lib下的這兩個檔案複製到Web應用的WEBINF\lib路徑下。
2.JSP頁面
為了實現檔案上傳,需要將檔案域所在的表單的enctype屬性設定為"multipart/form-data",該頁面的程式碼如下:
<%@ page language="java" contentType="text/html; UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><s:text name="authPrivacy"/> </title>
</head>
<body>
<s:form action="uploadPro" enctype="multipart/form-data" theme="simple">
<s:file name="upload"></s:file><br/>
<s:submit value="上傳"></s:submit>
</s:form>
</body>
</html>
其中<s:file>用於生成一個檔案上傳域
3.Action類
public class UploadAction extends BaseAction{
private File upload;
private String uploadContentType;
private String uploadFileName;
private String savePath;
private String userName;
private UserService userService;
private UserInfo user;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public File getUpload() {
return upload;
}
public void setUpload(File upload) {
this.upload = upload;
}
public String getUploadContentType() {
return uploadContentType;
}
public void setUploadContentType(String uploadContentType) {
this.uploadContentType = uploadContentType;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
public String getSavePath() {
return ServletActionContext.getServletContext().getRealPath("/"+savePath);
}
public void setSavePath(String savePath) {
this.savePath = savePath;
}
public UserService getUserService() {
return userService;
}
public void setUserService(UserService userService) {
this.userService = userService;
}
public UserInfo getUser() {
return user;
}
public void setUser(UserInfo user) {
this.user = user;
}
public String execute() {
FileOutputStream fos;
try {
String newFileName = this.getSession().getServletContext().getRealPath("/")+savePath+"\\"+ uploadFileName;
fos = new FileOutputStream(newFileName);
FileInputStream fis = new FileInputStream(getUpload());
byte[] buffer = new byte[1024];
int len = 0;
while((len = fis.read(buffer)) > 0){
fos.write(buffer,0,len);
}
return SUCCESS;
} catch (FileNotFoundException e) {
e.printStackTrace();
return ERROR;
} catch (IOException e) {
e.printStackTrace();
return ERROR;
}
}
}
上述Actiion使用三個屬性來封裝檔案域:檔名、檔案型別和檔案內容
4.配置檔案上傳的Action
<action name="uploadPro" class="com.b2d2.account.action.UploadAction">
<param name="savePath">upload</param>
<result name="success">/Views/account/success.jsp</result>
</action>
使用<param>引數設定UploadAction的savePath屬性值
<%@ page language="java" contentType="text/html; UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="s" uri="/struts-tags" %>
5.success.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title><s:text name="loginPage"/> </title>
</head>
<body>
<table border="0">
<tr>
<td></td>
<td><img name="portrait" src="<s:property value="'upload/'+uploadFileName"/>" width="100" height="140" alt="頭像" style="background-color: #FFCCFF" /></td>
</tr>
<tr>
<td></td>
<td><a href="upload.action">修改頭像</a></td>
</tr>
</table>
</body>
</html>
使用<s:property>標籤來獲取使用者的頭像,uploadFileName為UploadAction繫結的uploadFileName屬性