Struts2实现单个文件上传

首先需要两个jar文件:commons-fileupload-x.x.x jarcommons-io-x.x.x jar
一般在pom.xml中导入struts2-core后会包含以上两个jar文件

目录结构:
在这里插入图片描述
upload.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>文件上传</title>
</head>
<%@taglib uri="/struts-tags" prefix="s"%>
<body>
    <s:form action="upload.action" enctype="multipart/form-data" method="POST">
        <s:textfield name="title" label="标题"/><br>
        <s:file name="upload" label="选择文件"/><br>
        <s:submit name="submit" value="上传文件"/>
    </s:form>
</body>
</html>

UploadAction

package cn.wgb.action;

import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;

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

public class UploadAction extends ActionSupport {

    //封装上传到服务器的文件对象
    private File upload;

    //封装上传文件的类型
    private String uploadContentType;

    //封装上传文件名称
    private String uploadFileName;

    //获取上传文件路径,是应用上下文中的相对路径
    private String savePath;

    @Override
    public String execute() throws Exception {

        byte[] buffer = new byte[1024];

        //读取文件
        FileInputStream fis = new FileInputStream(getUpload());

        //保存文件
        FileOutputStream fos = new FileOutputStream(getSavePath()+"\\"+this.getUploadFileName());

        int length = fis.read(buffer);

        while (length>0){
            //每次写入length长度的内容
            fos.write(buffer,0,length);
            length = fis.read(buffer);
        }

        fis.close();
        fos.flush();
        fos.close();

        return SUCCESS;
    }

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

    public String getSavePath() {

        //通过读取存放目录获得保存文件的绝对路径
        return ServletActionContext.getServletContext().getRealPath(savePath);
    }

    public void setSavePath(String savePath) {

        this.savePath = savePath;
    }
}

在Action中使用了三个属性来封装文件信息如下,需要注意的是:

  • File类型的xxx属性:与表单中的File控件的name属性一致,用于封装File控件对应的文件内容。
  • String类型的xxxFileName属性:该属性名称由前面的File类型属性和FileName组合而成,是固定的语法,其作用是封装File控件对应的文件名。
  • String类型的xxxContentType属性:同样由xxx属性和ContentType组合而成,是固定的语法,其作用是封装File控件对应文件的文件类型。
    有了这三个属性,在上传文件时就可以直接通过get方法获取文件名和类型及文件内容。

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>

    <constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

    <!--创建一个default包,继承自Struts2的struts-default包-->
    <package name="default" namespace="/" extends="struts-default">

        <action name="upload" class="cn.wgb.action.UploadAction">
            <!--通过param参数设置保存目录的路径-->
            <param name="savePath">/upload</param>
            <result name="success">/upload_success.jsp</result>

        </action>

    </package>
</struts>

upload_success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>文件上传成功</title>
</head>
<%@taglib uri="/struts-tags" prefix="s"%>
<body>
    您所上传的文件是:<s:property value="uploadFileName"/><br>
    文件类型:<s:property value="uploadContentType"/>
</body>
</html>

web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

  <welcome-file-list>
    <welcome-file>upload.jsp</welcome-file>
  </welcome-file-list>

</web-app>

运行结果如图:
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_41385798/article/details/83576921