struts文件上传

一、编写上传界面

<body>
  <form action="${pageContext.request.contextPath}/fileUploadAction" method="post" enctype="multipart/form-data">
      文件:<input type="file" name="file1"><br />
      <input type="submit" value="上传">
  </form>
</body>

二、编写后台Action

public class FileUpload extends ActionSupport{
    //文件:<input type="file" name="file1"><br />
    private File file1;
    private String file1FileName;
    private    String file1ContentType;
    
    public void setFile1(File file1) {
        this.file1 = file1;
    }
    public void setFile1FileName(String file1FileName) {
        this.file1FileName = file1FileName;
    }
    public void setFile1ContentType(String file1ContentType) {
        this.file1ContentType = file1ContentType;
    }
    
    @Override
    public String execute() throws Exception {
        /*****处理业务******/
        //吧文件上传到upload目录
        //获取上传的路径
        String path = ServletActionContext.getServletContext().getRealPath("/upload");
        File destFile = new File(path,file1FileName);
        FileUtils.copyFile(file1, destFile);
        return SUCCESS;
    }
}

三、编写配置文件upload.xml(注意:class值用点分割)

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
          "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <package name="upload" extends="struts-default">
    <!-- 注意class使用点 -->
        <action name="fileUploadAction" class="cn.itcast.e_fileupload.FileUpload">
            <result name="success">/e/success.jsp</result>
            <!-- 配置错误视图 -->
            <result name="input">/e/error.jsp</result>
        </action>
    </package>
</struts>

四、把upload.xml引入struts.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
          "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <!-- 总的配置文件,引入其他文件 -->
    <include file="cn/itcast/e_fileupload/upload.xml"></include>
</struts>

五、配置(上传文件大小,以及类型)

1、struts默认上传大小是2M;当文件大于2M就会报错,此时可以配置错误视图error.jsp(可以显示错误)。

 <body>
  error.jsp<br />
  <!-- 查看错误信息 -->
  <%@ taglib uri="/struts-tags" prefix="s" %>
  <s:fielderror></s:fielderror>
 </body>

2、配置上传文件大小(struts-core-2.3.4.1.jar/org.apache.struts2/static/default.properties此文件包含常量设置)

    在src目录下,新建constant.xml

<struts>
    <!-- 全局配置 -->
    
    <!-- 上传文件大小配置 :30M-->
    <constant name="struts.multipart.maxSize" value="31457280" />
</struts>

3、配置上传文件类型(配置拦截器)

<struts>
    <package name="upload" extends="struts-default">
    <!-- 注意class使用点 -->
        <action name="fileUploadAction" class="cn.itcast.e_fileupload.FileUpload">
            <!-- 配置文件上传的类型 -->
            <interceptor-ref name="defaultStack">
                <param name="fileUpload.allowedExtensions">txt,jpg</param>
            </interceptor-ref>
            <result name="success">/e/success.jsp</result>
            <!-- 配置错误视图 -->
            <result name="input">/e/error.jsp</result>
        </action>
    </package>
</struts>

 

posted on 2017-01-31 21:43  hgfs瑞  阅读(183)  评论(0编辑  收藏  举报

导航