struts2第二次课

一实现上传

1.拷贝common提供的fileupload组件(2个jar)到/WEB-INF/lib目录中。
  a.commons-fileupload-1.2.jar       b。commons-io-1.3.1.jar

2 在Action中写:

    private File upload;

    private String uploadFileName  

    public String upload() throws Exception
   {
      HttpServletRequest request=ServletActionContext.getRequest();
      String targetFile = request.getRealPath("/upload")+"/"+this.uploadFileName;
  
     FileInputStream in=new FileInputStream(upload);
     FileOutputStream out = new FileOutputStream(targetFile);
     byte[] buffer=new byte[100*1024];
     int length=-1;
     while((length=in.read(buffer))>0)

     {
      out.write(buffer, 0, length);
     }
     out.close();
     in.close();
     return "success";
 }

 

3.在strutx.xml中声明上传组件的类型.
<constant name="struts.multipart.parser" value="jakarta"></constant>
//设定上传文件大小的上限,默认为2M
<constant name="struts.multipart.maxSize" value="209715200"></constant>

//设定请求的编码,处理中文文件名
<constant name="struts.i18n.encoding" value="gbk"></constant>  

 

 

   二:整合spring

1 给项目添加spring

2 将整合spring的插件struts-spring-plugin.jar到/WEB-INF/lib目录下。
3 在web.xml中配置

<context-param>
  <param-name>contextConfigLocation</param-name><param-value>/WEB- INF/classes/applicationContext.xml </param-value>
</context-param>


<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值