struts2之文件上传

1.需求

1.1使用struts2实现文件上传功能

2.实现

2.1 首先实现action类

package com.chet.lab5;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

public class FileUploadAction extends ActionSupport {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    //文件上传1.实现上传控制器
    //提交过来的文件 与文件框同名
    private File file;
    //提交过来的文件名  必须为这个,值有struts的拦截器提供
    private String fileFileName;
    //提交过来的文件类型 必须为这个,值有struts的拦截器提供
    private String fileContentType;

    @Override
    public String execute() throws Exception {
        InputStream in = new FileInputStream(file);
        String uploadPath = ServletActionContext.getServletContext().getRealPath("/upload");
        File tofile = new File(uploadPath,this.getFileFileName());
        OutputStream  out = new FileOutputStream(tofile);
        byte[] buffer = new byte[1024];
        int length = 0;
        while(-1!=(length=in.read(buffer,0,buffer.length)))
        {
            out.write(buffer);
        }

        in.close();
        out.close();
        return super.execute();
    }

    @Override
    public String input() throws Exception {
        return super.input();
    }


    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;
    }



}

2.2配置struts2.xml 支持的文件类型可以在tomcat的conf路径下的web.xml文件查询

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>
    <constant name="struts.custom.i18n.resources" value="com.chet.lab5.FileUploadMessage"></constant>
    <package name="lab5" extends="struts-default" namespace="/">
        <action name="uploadfile" class="com.chet.lab5.FileUploadAction">
            <result name="success">/result.jsp</result>
            <result name="input">/index.jsp</result>
            <interceptor-ref name="defaultStack">
                <param name="fileUpload.maximumSize">4194304</param>
                <param name="fileUpload.allowedExtendsize">.docx,.ppt,.txt,.jpg</param>
                <param name="fileUpload.allowedTypes">text/plain,application/vnd.openxmlformats-officedocument.wordprocessingml.document,image/jpeg,application/vnd.ms-powerpoint</param>
            </interceptor-ref>
        </action>
    </package>
</struts>

2.3 上传页面

<body>
    <s:form action="uploadfile" method="post" enctype="multipart/form-data">
        <s:fielderror name="file"></s:fielderror>
        <s:file name="file" label="请选择上传文件" labelposition="left"></s:file>
        <s:submit value="上传"></s:submit>
    </s:form>
  </body>

2.4值得注意的是,当上传文件的类型不符合要求,那么将不会进入你自定义的Action而是由上传文件拦截器,把控制权交至默认的拦截器下,并返回了result=“input” 再由struts页面分发器,转至对应的页面。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值