美文网首页
Struts2文件上传

Struts2文件上传

作者: 暗香抚动 | 来源:发表于2016-10-14 17:42 被阅读0次

Struts2通过FileUploadInterceptor拦截器实现文件上传,demo如下:

  • 先上struts.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>
    <package name="up" namespace="/u" extends="struts-default">
    <action name="uploadAction" class="com.wxhl.upload.UploadAction" method="upLoad">
    <interceptor-ref name="defaultStack">

    <param name="fileUpload.allowedExtensions">txt,jpg</param>

    <param name="fileUpload.allowedType">text/plain,image/jpeg</param>
    </interceptor-ref>
    <result name="UP">/updown/index.jsp</result>
    <result name="input">/updown/error.jsp</result>
    </action>
    </package>
    </struts>

  • 其次是jsp页面:
    <body>
    <div style="text-align:center">
    <h1>文件上传页面</h1>
    <form action="${pageContext.request.contextPath}/u/uploadAction"
    method="post" enctype="multipart/form-data">
    用户名:<input type="text" name="userName"/>

    文件名:<input type="file" name="file1"/>

    <input type="submit" value="上传"/>
    </form>
    </div>
    </body>
    注意:form中必须设置enctype的值为multipart/form-data

  • 然后是action类:
    public class UploadAction extends ActionSupport{
    private static final long serialVersionUID = -2070240561299671465L;
    //获取文件对应表单<input type="file" name="file1"/>
    private File file1;
    //获取文件名字
    private String file1FileName;
    //获取文件类型
    private String file1ContentType;

      public File getFile1() {
        return file1;
      }
    
      public void setFile1(File file1) {
        this.file1 = file1;
      }
    
      public String getFile1FileName() {
        return file1FileName;
      }
    
      public void setFile1FileName(String file1Name) {
        this.file1FileName = file1Name;
      }  
    
      public String getFile1ContentType() {
        return file1ContentType;
      }
    
      public void setFile1ContentType(String file1Type) {
         this.file1ContentType = file1Type;
      }
    
      public String upLoad(){
        //获取文件存放路径
        String path = ServletActionContext.getServletContext().getRealPath("/upload");
        //System.out.println(path);
        //新建文件
        File newFile = new File(path,file1FileName);
        //复制文件
        try {
          FileUtils.copyFile(file1, newFile);
        } catch (IOException e) {
          e.printStackTrace();
        }
        return "UP";
      }  
    }  
    

注意:此处File对象的名字必须与文件名的name保持一直,两者只有一个字段file1保持一直,这就是FileUploadInterceptor的功劳

  • 总结
    你所要做的只是按照一定的样式命名这三个字段的set方法,而字段名可以任意命名。第一个File类型的字段的set方法还是以常规的方式命名,另两个String类型的字段(文件名和文件类型)的set方法,必须分别以“set+File字段的名字+FileName”和“set+File字段的名字+ContentType”来命名。

相关文章

  • 文件上传

    Struts2框架默认采用Commons-fileupload组件完成文件上传功能。 • 使用Struts2框架实...

  • Struts下

    Struts2 第三天 学习目标 在struts2框架下怎么实现文件的上传下载 在struts2框架中怎么使用js...

  • Struts2文件上传

    Struts2通过FileUploadInterceptor拦截器实现文件上传,demo如下: 先上struts....

  • Struts2拦截器详解

    在 Struts2 框架中,拦截器是其重要的组成部分,Struts2 的很多功能(数据校验、对象类型转换、文件上传...

  • struts2进行文件的upload和download的问题

    今天在学习使用struts2进行文件的upload和download的时候遇到了许多问题。 总结如下: 文件上传临...

  • Struts2框架实现文件上传

    Struts2框架实现文件上传 开发环境系统:Windows10 版本1709JDK:1.8.0_161 32位I...

  • 文件上传与下载(四)Struts2 实现

    这一部分使用Struts2实现文件的上传与下载。 必须条件:前台form表单 method="post" enct...

  • struts2文件上传

    准备commons-fileupload和commons-io这两个jar包,并配置struts环境 编写简单的上...

  • struts2文件上传

    前台准备 表单必须是post提交 提交类型为enctype,多段式提交 使用 组件 后台接受

  • Struts2拦截器Interceptor

    拦截器是Struts2框架的核心,它主要完成解析请求参数、将请求参数赋值给Action属性、执行数据校验、文件上传...

网友评论

      本文标题:Struts2文件上传

      本文链接:https://www.haomeiwen.com/subject/vdmgyttx.html