struts2-14文件上传

文件上传:
第一步:在WEB-INF/lib下加入commons-fileupload-1.2.1.jar、common-io-1.3.2.jar,这两个文件可以在http://commons.apache.org下载。
第二步:把form表单的enctype设置为“multipart/form-data”,如下:

<form enctype = "multipart/form-data" action = "${pageContext.request.contextPath}/xxxx.action" method = "post">
    <input type = "file" name = "uploadImages">
</form>

第三步:在Action中添加以下属性:(加*的部分要跟页面中file的name属性相同)

public class HelloWorldAction(){
    public File *uploadImages*;//获取上传的文件
    public String *uploadImages*ContextType;//获取文件的类型
    public String *uploadImages*FileName;//获取文件的名称

    //添加属性的getter/setter方法。

    public String uploads() throws Exception{
        String realPath = ServletActionContext.getServletContext().getRealPath("/images");
        File file = new File(realPath);
        if(!file.exists())
            file.mkdirs();
        FileUtils.copyFile(uploadImages,new File(file,uploadImagesFileName));
        return "success";
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值