这些天用到在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属性