struts2学习之文件的上传与下载

单文件上传

SingleUploadAction.java

public class SingleUploadAction extends ActionSupport implements Serializable {

    private static final long serialVersionUID = -1732593542292662802L;
    //对应表单输入域name="image",Struts2框架会封装成File类型的
    private File image;
    //上传文件的名称,自定义的image和FileName的组合,由struts封装
    private String imageFileName;
    //上传文件的MIME类型,自定义的image和ContentType的组合,由struts封装
    private String imageContentType;    

    public String singleUpload() {
        try {
            //得到文件名
            String filename = this.imageFileName;
            //得到ServletContext对象的引用
            ServletContext context = ServletActionContext.getServletContext();
            //构建存储路径
            String savepath = context.getRealPath("/upload");                       
            //读写入数据,把上传文件拷贝到新建目录        
            FileUtils.copyFile(image, new File(savepath, filename));

            ActionContext.getContext().put("message", "上传成功!");
            return SUCCESS;
        } catch (Exception e) {
            e.printStackTrace();
            ActionContext.getContext().put("message", "上传失败!");
            return ERROR;
        }
    }


    public File getImage() {
        return image;
    }


    public void setImage(File image) {
        this.image = image;
    }


    public String getImageFileName() {
        return imageFileName;
    }


    public void setImageFileName(String imageFileName) {
        this.imageFileName = imageFileName;
    }


    public String getImageContentType() {
        return imageContentType;
    }


    public void setImageContentType(String imageContentType) {
        this.imageContentType = imageContentType;
    }
}

upload.jsp

普通单文件上传表单

<b>普通表单单文件上传</b><br/>
<form action="${pageContext.request.contextPath }/upload/singleupload" method="post" enctype="multipart/form-data">
    文件:<input type="file" name="image"/><br/>   
    <input type="submit" value="上传">
</form>

struts2单文件上传表单

<b>struts表单单文件上传</b>
<s:form action="singleupload" namespace="/upload" enctype="multipart/form-data">
    <s:file name="image" label="文件"></s:file>

    <s:submit value="上传"/>
</s:form>

两个表单都必须添加enctype=”multipart/form-data”属性,否则会出错,无法上传。最好在里也添加method=”post”,不易出问题。

struts.xml

省略DTD部分。

<!-- 限制文件上传大小5M -->
<constant name="struts.multipart.maxSize" value="5242880"></constant>

<package name="upload" namespace="/upload" extends="struts-default">
    <action name="singleupload" class="com.struts2.action.example.SingleUploadAction" method="singleUpload">
        <result name="success">/WEB-INF/jsp/success.jsp</result>
    </action>               
</package>

回执页面uploadsuccess.jsp

文件名:<s:property value="imageFileName"/><br>
文件类型:<s:property value="imageContentType"/><br/>
<br/>

多文件上传

MoreUploadAction.java

public class MoreUploadAction extends ActionSupport implements Serializable {

    private static final long serialVersionUID = 7962335953329769349L;

    private File images[];
    private String[] imagesFileName;
    private String[] imagesContentType;

    public File[] getImages() {
        return images;
    }
    public void setImages(File[] images) {
        this.images = images;
    }
    public String[] getImagesFileName() {
        return imagesFileName;
    }
    public void setImagesFileName(String[] imagesFileName) {
        this.imagesFileName = imagesFileName;
    }
    public String[] getImagesContentType() {
        return imagesContentType;
    }
    public void setImagesContentType(String[] imagesContentType) {
        this.imagesContentType = imagesContentType;
    }

    public String moreUpload() {

        try {           
            if(images!=null&&images.length>0){
                //得到ServletContext对象的引用
                ServletContext sc = ServletActionContext.getServletContext();
                //构建存储路径
                String storePath = sc.getRealPath("/files");
                for(int i=0;i<images.length;i++)
                    FileUtils.copyFile(images[i], new File(storePath,imagesFileName[i]));
            }

            ActionContext.getContext().put("message", "上传成功!");
            return SUCCESS;

            } catch (Exception e) {
                e.printStackTrace();
                ActionContext.getContext().put("message", "上传失败!");
                return ERROR;
            }

    }
}

即将属性改为数组即可。

upload.jsp

普通多文件上传表单

<b>普通表单多文件上传</b>
<form action="${pageContext.request.contextPath }/upload/moreupload" method="post" enctype="multipart/form-data">
    文件:<input type="file" name="images"/><br/>
    文件:<input type="file" name="images"/><br/>  
    文件:<input type="file" name="images"/><br/>      
    <input type="submit" value="上传">
</form>

struts2多文件上传表单

<b>struts表单多文件上传</b>
<s:form action="moreupload"  namespace="/upload" method="post" enctype="multipart/form-data">
    <s:file name="images" label="文件"></s:file>
    <s:file name="images" label="文件"></s:file>
    <s:file name="images" label="文件"></s:file>
    <s:submit value="上传"/>
</s:form>

struts.xml

<action name="moreupload" class="com.struts2.action.example.MoreUploadAction" method="moreUpload">
        <result name="success">/WEB-INF/jsp/success.jsp</result>
</action>

文件下载

客户端用户右键选择“另存为…”来下载,在回执页面添加如下代码:

<p>文件下载</p>
<s:a href="%{imageFileName}">普通下载[右键另存为]</s:a>

以上。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值