Struts2中多文件上传

文件上传大体我给分这么几步。1:在WEB-INF/lib下面添加commons-fileupload-1.2.1.jar,commons-io-1.3.2.jar,这两个文件全部在Struts2提供的lib包下面;2:在文件上传的表单处做设置,将form表单的enctype设置成为:“multipart/form-data”,提交方式设置为post;3:做Action。

具体代码如下:

[size=medium]fileupload.jsp[/size]

[img]http://dl.iteye.com/upload/attachment/206450/0eab6cca-492b-3700-a684-e1b99b67cb7b.jpg[/img]


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
${pageContext.request.contextPath}<br />
<form action="${pageContext.request.contextPath }/fileupload" enctype="multipart/form-data" method="post">
选择资源1:<input type="file" name="image"><BR />
选择资源2:<input type="file" name="image"><BR />
<input type="submit" value="上传"/>
</form>
</body>
</html>

[size=medium]struts.xml[/size]

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<package name="yuan" extends="struts-default">
<action name="fileupload" class="com.ambow.Action.FileUploadAction">
<result>
/WEB-INF/page/message.jsp
</result>
</action>
</package>
</struts>


[size=medium]FileUploadAction.java[/size]

package com.ambow.Action;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {

//这里是固定的写法,文件名子必须跟上一个表单的name属性名字相同,都是image
private File[] image ;
private String[] imageFileName ;



public File[] getImage() {
return image;
}

public void setImage(File[] image) {
this.image = image;
}

public String[] getImageFileName() {
return imageFileName;
}

public void setImageFileName(String[] imageFileName) {
this.imageFileName = imageFileName;
}

@Override
public String execute() throws Exception {
// TODO Auto-generated method stub
//得到当前工程的目录,然后自己再定义下保存的路径
String realpath = ServletActionContext.getServletContext().getRealPath("/file") ;
//打印路径看是否存在
System.out.println(realpath);
if(image != null){
File savedir = new File(realpath) ;
if(!savedir.exists()){
savedir.mkdirs() ;
}
for(int i=0;i<image.length;i++){
File savefile = new File(savedir,imageFileName[i]) ;
FileUtils.copyFile(image[i], savefile) ;
}
}
ServletActionContext.getServletContext().setAttribute("message", "文件上传成功!") ;
return SUCCESS;
}


}


[size=medium]message.jsp[/size]

[img]http://dl.iteye.com/upload/attachment/206452/825d4c2b-abfc-3fde-a1c0-809a8a4c3e50.jpg[/img]


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!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>Insert title here</title>
</head>
<body>
${message }
</body>
</html>

几点注意事项:在Action中的属性,比如image,都必须跟表单当中的属性值一样,都是image,还有属性的名字都必须按照固定的格式写,如image,imageFileName,如果不这么写,会报错的。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值