Struts2(15)之文件上传

Struts2(15)之文件上传

微信搜索 zze_coding 或扫描 👉 二维码关注我的微信公众号获取更多资源推送:

1、新建接收上传请求的 Action 类。

// com.zze.web.action.FileUploadAction

import com.opensymphony.xwork2.ActionSupport;
import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletResponseAware;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

public class FileUploadAction extends ActionSupport implements ServletResponseAware {
    /*
     为文件上传提供三个属性 :
        String 表单项 name + FileName : 接收文件名称
        File 表单项 name : 接收文件内容
        String 表单项 name + ContentType : 上传文件的 ContentType
      */
    private String fileFileName;

    private File file;

    private String fileContentType;

    public void setFileFileName(String fileFileName) {
        this.fileFileName = fileFileName;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public void setFileContentType(String fileContentType) {
        this.fileContentType = fileContentType;
    }

    private ServletResponse response;

    @Override
    public String execute() throws Exception {
        response.setContentType("text/plain;charset=utf8");
        String msg = "上传成功";
        System.out.println(fileFileName);
        System.out.println(file);
        System.out.println(fileContentType);
        // 存储路径
        String fullPath = "D://upload/"+fileFileName;
        File destFile = new File(fullPath);
        FileUtils.copyFile(file,destFile);
        response.getWriter().write(msg);
        return NONE;
    }

    @Override
    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
    }
}

2、配置 Action:

<!-- struts.xml -->
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
    <!--限制单次文件上传总大小不可大于 5m-->
    <constant name="struts.multipart.maxSize" value="5242880"/>
    <!--配置国际化资源文件名-->
    <constant name="struts.custom.i18n.resources" value="message"/>
    <package name="test" extends="struts-default" namespace="/">
        <action name="upload" class="com.zze.web.action.FileUploadAction">
            <result name="input">/index.jsp</result>
            <interceptor-ref name="defaultStack">
                <!--限制单个文件大小不超过 2m-->
                <param name="fileUpload.maximumSize">2097152</param>
                <!--限制文件后缀-->
                <param name="fileUpload.allowedExtensions">.jpg,.png</param>
                <!--限制文件的 MIME 类型-->
                <param name="fileUpload.allowedTypes">image/jpg,image/png</param>
            </interceptor-ref>
        </action>
    </package>
</struts>

3、新建文件上传页面:

<!-- index.jsp -->
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
    <title>Struts2 文件上传测试</title>
</head>
<body>
<form action="${pageContext.request.contextPath}/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="file">
    <input type="submit" value="上传">
    <s:actionerror/>
    <s:fielderror/>
</form>
</body>
</html>

4、修改 Struts2 内置的国际化错误提示信息:

struts.messages.error.uploading=\u4e0a\u4f20\u9519\u8bef
struts.messages.error.file.too.large=\u6587\u4ef6\u592a\u5927
struts.messages.error.content.type.not.allowed=\u8bf7\u9009\u62e9\u56fe\u7247\u6587\u4ef6
struts.messages.error.file.extension.not.allowed=\u8bf7\u9009\u62e9\u002e\u006a\u0070\u0067\u6216\u002e\u0070\u006e\u0067\u7ed3\u5c3e\u7684\u6587\u4ef6

效果如下:

image.png

struts2 文件上传功能是使用 commons-fileupload 实现。

Copyright: 采用 知识共享署名4.0 国际许可协议进行许可

Links: https://www.zze.xyz/archives/struts2-15.html

Buy me a cup of coffee ☕.