使用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屬性


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章