Struts2中文件上传和多文件上传以及获取HttpServletRequest / HttpSession / ServletContext / HttpServletRespons

2013-03-02 11:18113人阅读评论(0)收藏举报

1.文件上传

第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jarcommons-io-1.3.2.jar。这两个文件可以从http://commons.apache.org/下载。

第二步:把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 String upload() throws Exception{

String realpath = ServletActionContext.getServletContext().getRealPath("/images");

File file = new File(realpath);

if(!file.exists()) file.mkdirs();

FileUtils.copyFile(uploadImage, new File(file, uploadImageFileName));

return "success";

}

}

2.多文件上传

第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jarcommons-io-1.3.2.jar。这两个文件可以从http://commons.apache.org/下载。

第二步:把form表的enctype设置为:“multipart/form-data“,如下:

<form enctype="multipart/form-data" action="${pageContext.request.contextPath}/xxx.action" method="post">

<input type="file" name="uploadImages">

<input type="file" name="uploadImages">

</form>

第三步:在Action类中添加以下属性,属性红色部分对应于表单中文件字段的名称:

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";

}}

3.获取HttpServletRequest / HttpSession / ServletContext / HttpServletResponse对象

方法一,通过ServletActionContext.类直接获取:

public String rsa() throws Exception{

HttpServletRequest request = ServletActionContext.getRequest();

ServletContext servletContext = ServletActionContext.getServletContext();

request.getSession() 

HttpServletResponse response = ServletActionContext.getResponse();

return "scope";

}

方法二,实现指定接口,由struts框架运行时注入:

public class HelloWorldAction implements ServletRequestAware, ServletResponseAware, ServletContextAware{

private HttpServletRequest request;

private ServletContext servletContext;

private HttpServletResponse response;

public void setServletRequest(HttpServletRequest req) {

this.request=req;

}

public void setServletResponse(HttpServletResponse res) {

this.response=res;

}

public void setServletContext(ServletContext ser) {

this.servletContext=ser;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值