使用struts2标签上传用户头像(一)
来源:互联网 发布:使用动态规划的算法 编辑:程序博客网 时间:2024/05/16 09:38
这些天用到在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属性
- 使用struts2标签上传用户头像(一)
- 使用struts2标签上传用户头像(二)
- struts2实现头像上传
- struts2标签的使用(一)
- 使用Struts2的标签库(一)
- Yii上传文件(头像)详解(一)
- 使用node.js实现用户头像的上传
- Struts2标签使用一特例
- struts2无刷新上传头像
- 仿qq用户头像上传
- 使用Struts2标签实现文件上传
- Struts2-文件上传-<s:file>标签使用
- Struts2的CRUD(涵盖国际化、头像上传)
- 实现web版用户注册(可以上传头像)
- XMPP 客户端开发 ----- 用户上传头像(四)
- iOS AFNetworking 图片上传(修改用户头像功能)
- 用户上传用户头像至服务器
- Struts2.X标签(一)
- TableViewCell自定义分割线
- CSS使用技巧
- linux中shell变量$#,$@,$0,$1,$2的含义解释
- 计算两个GPS坐标的距离 方法二 - C语言
- Swift for 的几个例子
- 使用struts2标签上传用户头像(一)
- STM32F103RBT6 定时器 Timer2 源程序
- ASP.NET MVC的ContentResult
- HTML5基础(一)——元素、属性、格式化、样式、链接、表格、列表、块、布局
- MINA 2.0.9: 用户定义的事件是如何触发的
- WPF中Auto与*的区别
- abap create dynamic structure and dynamic table
- Linux tc性能测试
- Android网络操作之Volley