Struts2学习笔记之文件上传详解

作者:简简单单 2013-11-03

文件上传实例

1.前台页面表单内容如下,名字为upload.jsp
 

 代码如下 复制代码
Action" enctype="multipart/form-data" theme="xhtml" method="post">
   
   


2.struts.xml
里面配置一个action如下:

 代码如下 复制代码


          /welcom.jsp
          /input.jsp


3.相对应的Action如下:

 代码如下 复制代码

package org.Rudiment.action;


public class UploadAction extends ActionSupport
{
    //这个是必须的与前台的 对应
    private File upload; 
    //这个也是必须的命名规则xxxContentType;        
    private String uploadContentType;
    //这个也是必须的命名规则是xxxFileName;
    private String uploadFileName;


    public File getUpload() {
        return upload;
    }


    public void setUpload(File upload) {
        this.upload = upload;
    }


    public String getUploadContentType() {
        return uploadContentType;
    }


    public void setUploadContentType(String uploadContentType) {
        this.uploadContentType = uploadContentType;
    }


    public String getUploadFileName() {
        return uploadFileName;
    }


    public void setUploadFileName(String uploadFileName) {
        this.uploadFileName = uploadFileName;
    }


    @Override
    public String execute()
    {
        //文件上传处理代码
        try {
            FileOutputStream fos = new FileOutputStream(ServletActionContext.getServletContext().getRealPath("/") + getUploadFileName());
            FileInputStream fis = new FileInputStream(getUpload());
            byte[] buffer = new byte[1024];
            int len = 0;
           
            while((len = fis.read(buffer)) > 0)
            {
                fos.write(buffer, 0, len);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
       
        return SUCCESS;
    }
}

控制文件上传有两种方法:

第一种是在Action用自己代码进行判断控制。
第二中是基于strtus2框架的配置文件进行文件控制。


第一种方法:

要实现上传功能的Action必须要三个变量,如果不知道规范要求是怎么先看一下这篇文章。
struts2笔记之文件上传

按照上面文章的Action我们只需判断它的uploadFileName和uploadFileContentype就可以获取当前用户上传的文件类型。还有upload这个属性是File的实例,因此我们可以 upload.length(); 来判断上传文件的大小

下面是一个范例:

 代码如下 复制代码

    @Override
    public String execute()
    {
        //这里将要有用的变量内容打印出来
        System.out.println("FileSize:" + upload.length());
        System.out.println("uploadFileName:" + uploadFileName);
        System.out.println("uploadContentType:" + uploadContentType);
       
        //通过这两个变量我们就可以在execute中通过一下的方式来判断了。
        if(uploadContentType.equals("image/png") && upload.length() < 5000)
        {
            return INPUT;
        }
        else
        {
            return SUCCESS;
        }
       
    }

上面只是一个判断的我们自定义的判断逻辑框架。具体的上传逻辑没有写进去。具体上传逻辑在这篇文章可以找到
struts2笔记之文件上传

上面就是第一种方法了。第一中方法。灵活性比较强。手动检查有一个缺点就是当这种Action多了之后,可能维护工作会比较麻烦,因为要修改多个Action的execute()。
 

 
第二种方法:
 
只需要在execute写上传逻辑,不用理会上传文件是否符合规范。因为这些检查在strtus.xml中进行了配置,配置也是比较简单。

 代码如下 复制代码


                  
                       image/png
                       50000
                  

                  
                   /upload
                   /welcom.jsp
                   /login.jsp


在我们的Action配置中添加加粗的拦截器。则strtus会自动帮我们过滤不符合要求的上传文件。


注:由于我们显式指定了拦截器。所以struts默认拦截器不再生效。所以我们这里需要显式使用defaultStack这个拦截器,否则应用部署失败。

相关文章

精彩推荐