Struts2文件上传与下载

1.单文件上传

Action类:

public class UploadAction {
    private File file;//用于接收表单上传文件
    //xxxFileName为文件名,xxxContentType为文件类型,xxx为File变量名
    private String fileFileName;
    private String fileContentType;

    public String execute() throws IOException{
    //特别注意这里的getRealPath获取的路径是WebRoot下的文件夹路径
        File saved = new File(ServletActionContext.getServletContext().getRealPath("upload"),fileFileName);

        InputStream in = null;
        OutputStream out = null;

        try{
            in = new FileInputStream(file);
            out = new FileOutputStream(saved);

            byte[] b = new byte[1024];
            while((in.read(b)) != -1){
                out.write(b);
            }
        }catch(Exception e){

        }finally{
            in.close();
            out.close();
        }
        return "success";
    }

    public String getFile() {
        return file;
    }
    public void setFile(String file) {
        this.file = file;
    }
    public String getFileFileName() {
        return fileFileName;
    }
    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }
    public String getFileContentType() {
        return fileContentType;
    }
    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }

sturts.xml:

<struts>
    <package name="main" extends="struts-default" namespace="/">
        <action name="upload" class="com.sbw.action.UploadAction">
            <result name="success">/success.jsp</result>
        </action>
    </package>
</struts> 

login.jsp:

//注意上传文件需设置enctype和method为post
<s:form action="upload" enctype="multipart/form-data" method="post">
        <s:file name="file" label="文件"></s:file>
        <s:submit value="开始上传"/>
</s:form>

success.jsp:

文件已保存到:<a href="upload/<s:property value="fileFileName"/>" target="_blank"><s:property value="fileFileName"/></a>

运行结果:

这里写图片描述

2.多文件上传

Action类:

public class UploadAction {
    private File[] file;//用于接收表单上传文件
    //xxxFileName为文件名,xxxContentType为文件类型,xxx为File变量名
    private String[] fileFileName;
    private String[] fileContentType;

    public String execute() throws IOException{
    //特别注意这里的getRealPath获取的路径是WebRoot下的文件夹路径
        InputStream in = null;
        OutputStream out = null;
        try{
            for(int i = 0; i < file.length; i++){
                File saved = new File(ServletActionContext.getServletContext().getRealPath("upload"),fileFileName[i]);  
                in = new FileInputStream(file[i]);
                out = new FileOutputStream(saved);

                byte[] b = new byte[1024];
                while((in.read(b)) != -1){
                    out.write(b);
                }
            }
        }catch(Exception e){

        }finally{
            in.close();
            out.close();
        }

        return "success";
    }

    public File[] getFile() {
        return file;
    }
    public void setFile(File[] file) {
        this.file = file;
    }
    public String[] getFileFileName() {
        return fileFileName;
    }
    public void setFileFileName(String[] fileFileName) {
        this.fileFileName = fileFileName;
    }
    public String[] getFileContentType() {
        return fileContentType;
    }
    public void setFileContentType(String[] fileContentType) {
        this.fileContentType = fileContentType;
    }

login.jsp:

<s:form action="upload" enctype="multipart/form-data" method="post">
        <s:file name="file" label="文件1"></s:file>
        <s:file name="file" label="文件2"></s:file>
        <s:submit value="开始上传"/>
    </s:form>

success.jsp:

文件已保存到:<a href="upload/<s:property value="fileFileName[0]"/>" target="_blank"><s:property value="fileFileName[0]"/></a><br/>
        文件已保存到:<a href="upload/<s:property value="fileFileName[1]"/>" target="_blank"><s:property value="fileFileName[1]"/></a>

运行结果:

这里写图片描述

3.文件下载

Action类:

public class DownloadAction {
    private String path;

    public String execute(){
        return "success";
    }

    //方法名去掉get后的downloadFile要与struts.xml中配置的inputName相同
    public InputStream getDownloadFile(){
    //此处获得的路径是在Webroot根目录下的
        InputStream in = ServletActionContext.getServletContext().getResourceAsStream(getPath());

        return in;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }


}

sturts.xml:

<struts>
    <package name="main" extends="struts-default" namespace="/">
        <action name="download" class="com.sbw.action.DownloadAction">
            <param name="path">download/123.txt</param>
            <result name="success" type="stream">
                    <!-- 指定下载文件的类型 -->
                    <param name="contentType">image/gif</param>
                    <!-- 与action中getDownloadFile()去去掉get相同 -->                  
                    <param name="inputName">downloadFile</param>
                    <!-- 指定下载文件的位置 -->
                    <param name="contentDisposition">attachement;filename="123.txt"</param>

                    <!-- 指定下载文件的缓冲大小 -->
                    <param name="bufferSize">1024</param>
            </result>
        </action>
    </package>
</struts>

login.jsp

<a href="download.action">文件下载</a>

运行结果:

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值