《struts2权威指南》学习笔记之struts2文件上传

struts2没有提供自己的请求解析器,也就是说,struts2不会自己区处理multipart/form-data的请求,它需要调用其他请求解析器,将HTTP请求中的表单域解析出来,但struts2在原有的上传解析器上作了进一步封装,更进一步简化了文件上传

 Struts2的struts.properties配置文件中,配置struts2的上传文件解析器

struts.multipart.parser=jakarta  (srtuts2默认),也可以设置为常用的cos,pell等

 

配置上传页面:

< html  xmlns ="http://www.w3.org/1999/xhtml" >
< head >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=GBK"   />
< title > 简单的文件上传 </ title >
</ head >
< body >
< form  action ="upload.action"  method ="post"  enctype ="multipart/form-data" >
      文件标题:
< input  type ="text"  name ="title"   />< br >
      选择文件:
< input  type ="file"  name ="upload"   />< br >
    
< input  value ="上传"  type ="submit"   />
</ form >
</ body >
</ html >

 

配置上传action

 

package  lee;

import  com.opensymphony.xwork2.Action;
import  org.apache.struts2.ServletActionContext;
import  java.io.File;
import  java.io. * ;

import  com.opensymphony.xwork2.ActionSupport;



public   class  UploadAction  extends  ActionSupport
{
    
private String title;
    
private File upload;
    
private String uploadContentType;
    
private String uploadFileName;

    
//接受依赖注入的属性
    private String savePath;
    
//接受依赖注入的方法
    public void setSavePath(String value)
    
{
        
this.savePath = value;
    }


    
private String getSavePath() throws Exception 
    
{
        
return ServletActionContext.getRequest().getRealPath(savePath);
    }

    
    
public void setTitle(String title) {
        
this.title = title; 
    }


    
public void setUpload(File upload) {
        
this.upload = upload; 
    }


    
public void setUploadContentType(String uploadContentType) {
        
this.uploadContentType = uploadContentType; 
    }


    
public void setUploadFileName(String uploadFileName) {
        
this.uploadFileName = uploadFileName; 
    }


    
public String getTitle() {
        
return (this.title); 
    }


    
public File getUpload() {
        
return
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值