Struts2文件的上传

struts2文件上传

第一步: WEB-INF/lib 下加入 commons-fileupload-1.2.1.jar commons-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 类中添加以下属性, 属性红色部分对应于表单中文件字段的名称

private File uploadImage;//得到上传的文件

privateString uploadImage ContentType ;// 得到文件的类型
privateString uploadImage FileName ;// 得到文件的名称
(也可以这样说:
private String title;//封装标题请求参数的属性
private File upload;//封装上传文件域的属性

private String uploadContentType;//封装上传文件类型的属性
private String uploadFileName;//封装上传文件名的属性
private String savePath;直接在struts.xml文件中配置的属性


// 这里略省了属性的 getter/setter 方法
    public String upload() throws Exception{
        FileInputStream fis = new FileInputStream(getUpload());
        FileOutputStream fos = new FileOutputStream(getSavePath()+"\\"+getUploadFileName());
        byte[] buffer = new byte[1024];
        int len = 0;
        while((len=fis.read(buffer))>0){
            fos.write(buffer,0,len);
        }
        fis.close();
        fos.close();
        return     SUCCESS;
    }

struts2多文件上传

注:多文件上传只需将红色字体代码发生改变(这里略省了属性的getter/setter方法)

    private String title;
    private File[] upload;
    private String[] uploadContentType;
    private String[] uploadFileName;
    private String savePath;

    public String upload() throws Exception {
        for (int i = 0; i < upload.length; i++) {
            
                FileInputStream fis = new FileInputStream(getUpload()[i]);
                FileOutputStream fos = new FileOutputStream(getSavePath()+ "\\" +getUploadFileName()[i]);
                byte[] buffer = new byte[1024];
                int len = 0;
                while ((len = fis.read(buffer)) > 0) {
                    fos.write(buffer, 0, len);
                }
                fos.close();
                is.close();
        }
        return SUCCESS;    
    }

附:

struts中savepath配置,getter方法的更改及国际化配置


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <package name="dat" namespace="/dat" extends="struts-default">
      <action name="trans" class="action.UploadAction" method="upload">
      <param name="savePath">/uploadFiles</param>
        <result name="success">/success.jsp</result>
      </action>
    </package>
</struts>


public String getSavePath() {
        return ServletActionContext.getServletContext().getRealPath(savePath);
    }



在struts.xml中添加<constant name="struts.custom.i18n.resources" value="mess"/>

编写mess.properties配置文件:

struts.messages.error.content.type.not.allowed=\u6587\u4EF6\u683C\u5F0F\u6709\u8BEF\uFF01
struts.messages.error.file.too.large=\u6587\u4EF6\u592A\u5927\uFF01




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值