java action 上传文件_java 文件上传 struts2.0实现

1. web.xml  写全文拦截器

WEBROOT--- WEB-INF ---- WEB.XML

Struts2

org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter

Struts2

/*

2.写个文件上传页面

WEBROOT--E文件夹下----- upload.jsp

用户名:
文件:

3. 项目文件下     struts.xml

4.  项目src下   建立包: cn.itcast.e_fileupload      下 新建  upload.xml

/p>

"http://struts.apache.org/dtds/struts-2.3.dtd">

/e/success.jsp

5. cn.itcast.e_fileupload 下新建 fileupload.java

packagecn.itcast.e_fileupload;importjava.io.File;importorg.apache.commons.io.FileUtils;importorg.apache.struts2.ServletActionContext;importcom.opensymphony.xwork2.ActionSupport;public class fileupload extendsActionSupport{privateFile file1;privateString file1FileName; //该变量 固定格式 "file1" + FileName(注意大小写)privateString file1ContextType; //该变量 固定格式public voidsetFile1(File file1) {this.file1 =file1;

}public voidsetFile1filename(String file1FileName) {this.file1FileName =file1FileName;

}public voidsetFile1contexttype(String file1ContextType) {this.file1ContextType =file1ContextType;

}

@Overridepublic String execute() throwsException {

String path=ServletActionContext.getServletContext().getRealPath("/upload");

File desfile=newFile(path,file1FileName);

FileUtils.copyFile(file1, desfile);returnSUCCESS;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值