第一步:在WEB-INF/lib下加入commons-fileupload-X.X.X.jar、commons-io-X.X.X.jar。
第二步:把form表的enctype设置为:“multipart/form-data“,如下:
<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post">
<input type="file"name="uploadImage">
</form>
第三步:在Action类中添加以下属性,属性名字对应于表单中文件字段的名称:
public class HelloWorldAction{
private File uploadImage;//得到上传的文件
private String uploadImageContentType;//得到文件的类型
private String uploadImageFileName;//得到文件的名称
//这里略省了属性的getter/setter方法
public Stringupload() throws Exception{
Stringrealpath = ServletActionContext.getServletContext().getRealPath("/images");
Filefile = new File(realpath);
if(!file.exists()) file.mkdirs();
FileUtils.copyFile(uploadImage,new File(file, uploadImageFileName));
return"success";
}
}
例外,如果是多文件上传的话接收时候改为数组就可以,例如这样
public class HelloWorldAction{
private File[] uploadImages;//得到上传的文件
private String[] uploadImagesContentType;//得到文件的类型
private String[] uploadImagesFileName;//得到文件的名称
//这里略省了属性的getter/setter方法
public String upload() throws Exception{
String realpath = ServletActionContext.getServletContext().getRealPath("/images");
File file = new File(realpath);
if(!file.exists()) file.mkdirs();
for(int i=0 ;i<uploadImages.length; i++){ File uploadImage = uploadImages[i];
FileUtils.copyFile(uploadImage, new File(file, uploadImagesFileName[i]));
}
return "success";
}}
一般流程就是这样,现在举个这样的例子:像这样的博客是怎样更改头像的。
先看效果图
表单没什么注意可说的,记住加enctype="multipart/form-data就好,主要代码如下:
<form action="<%=this.getServletContext().getContextPath() %>/userProfile_uploadPhoto.action"
method="post" enctype="multipart/form-data">
<table width="100%" border="0" cellspacing="1"
cellpadding="10">
<tr>
<td width="40%"> <font class="word2">当前头像</font>
</td>
<td> <font class="word2">上传头像 </font>
</td>
</tr>
<tr>
<td valign="top"> <font class="word3">你可以到你的<a
href="" class="xh">头像相册</a>里选择照片设置为当前头像
</font> <br /> <img
/images/"+user.getId()+"/uploadPhoto"
src="<%=this.getServletContext().getContextPath()%>/images/${sessionScope.user.id}/uploadPhoto/${sessionScope.user.photo}?uploadP=<%=Math.random() %>"
width="200" height="200" />
</td>
<td valign="top"> <font class="word3">上传真实头像,成为<a
href="" class="xh">星级用户</a>,即可拥有无限量相册空间!
</font> <br /> <input type="file" class="wby"
name="userForm.uploadImage" /> <br /> <font class="word3">支持
JPG、JPEG、GIF、BMP 和 PNG 文件格式,最大4M。 头像高度超出宽度1.5倍的部分会被切除。</font> <br />
<br /> <input type="submit" value="上传头像" class="sub" />
</td>
</tr>
</table>
</form>
Action的主要代码:
public String uploadPhoto() {
Users user = null;
try {
user = (Users) ActionContext.getContext().getSession().get("user");
} catch (Exception e) {
return "";
}
//定义上传的头像到指定的服务器路径中,
String realPath = ServletActionContext.getServletContext().getRealPath(
"/images/" + user.getId() + "/uploadPhoto");
if (userForm.getUploadImage() != null) {
// 统一图片名字
int pointIndex = userForm.getUploadImageFileName().indexOf(".");
String extentName = userForm.getUploadImageFileName().substring(
pointIndex, userForm.getUploadImageFileName().length());
File saveFile = new File(realPath, "touxiang" + extentName);
//更改Session中用户的头像名字
user.setPhoto("touxiang" + extentName);
//把更新后的用户信息保存到数据库
userService.update(user);
//把更新后的用户信息重新加入到Session中
ActionContext.getContext().getSession().remove("user");
ActionContext.getContext().getSession().put("user", user);
//如果保存的目录不存在就直接冲减
if (!saveFile.getParentFile().exists()) {
saveFile.getParentFile().mkdirs();
}
//保存文件到服务器
try {
FileUtils.copyFile(userForm.getUploadImage(), saveFile);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return "goTouploadPhotoUi";
}
还要在struts.xml中的配置Action,这里就不写了。
又完成一篇笔记。