Struts2 实现多文件上传

本文介绍如何使用Struts2框架实现文件上传功能。通过设置表单属性并配置Struts2,可以轻松处理多文件上传任务。文章详细展示了从前端表单到后端处理的完整流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前台form 表单:设置method=post,enctype=multipart/form-data。

struts2在原有的上传解析器继承上做了进一步封装,更进一步简化了文件上传。

Action需要使用3个属性来封装该文件域的信息:

(1)类型为File的*属性封装了该文件域对应的文件内容;
(2)类型为String的***FileName属性封装了该文件域对应的文件的文件类型;
(3)类型为String的***ContentType属性封装了该文件域对应的文件的类型。

具体实现:

  1. 新建web项目

    这里写图片描述

  2. 添加struts2相关包
    myeclipse可直接下载,右击项目,如下。

    这里写图片描述

  3. 前台

    <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>
    <html>
      <body>
        <form action="upload.action" method="post" enctype="multipart/form-data">
            <input type="file" name="upload" multiple="multiple"/>
            <input type="submit" value="提交"/>
    
        </form>
      </body>
    </html>
  4. 配置web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
      <display-name>UploadFile</display-name>
      <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
      </filter>
      <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>*.action</url-pattern>
      </filter-mapping>
    </web-app>
  5. 配置struts.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
    <struts>
        <constant name="struts.enable.DynamicMethodInvocation" value="false"/>
        <constant name="struts.devMode" value="true"/>
        <constant name="struts.multipart.saveDir" value="/tmp"/>
        <constant name="struts.custom.i18n.resources" value="app"></constant>
    
        <package name="default" namespace="/" extends="struts-default">
            <action name="upload" class="com.yf.action.UploadAction">
            <result>/index.jsp</result>
            <interceptor-ref name="defaultStack"></interceptor-ref>
            </action>
        </package>
    </struts>    
    
  6. 后台代码
public class UploadAction extends ActionSupport{
    private List<File> upload;
    private List<String> uploadContentType;
    private List<String> uploadFileName;

    public List<File> getUpload() {
        return upload;
    }

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

    public List<String> getUploadContentType() {
        return uploadContentType;
    }

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

    public List<String> getUploadFileName() {
        return uploadFileName;
    }

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

    @Override
    public String execute() throws Exception {
        //文件保存路径
        String path = ServletActionContext.getServletContext().getRealPath("/images");
        File file = new File(path);
        //不存在则创建
        if(!file.exists()){
            file.mkdir();
        }
        //循环将文件上传到指定路径
        for(int i = 0; i< upload.size(); i++){
            FileUtils.copyFile(upload.get(i), new File(file,uploadFileName.get(i)));
        }
        return SUCCESS;
    }

结果如下
这里写图片描述

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值