struts文件上传(多文件)

第01步:配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" 
    xmlns="http://java.sun.com/xml/ns/j2ee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
    http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    
    <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>/*</url-pattern>
    </filter-mapping>
    
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

 

第02步:编写action类

package com.self.action;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;

/**文件上传**/
public class FileUpload {
    private File[] image;//得到上传的文件
    private String[] imageFileName;//获取上传文件的名称,命名规则:页面属性名+FileName
    private String[] imageContentType;//得到上传文件的类型
    
    public FileUpload() {
    }
    
    /**上传方法**/
    public String uploadFile(){
        try {
        String realPath=ServletActionContext.getServletContext().getRealPath("/images");
        System.out.println("工程路径(/images路径,文件保存路径):"+realPath);
        if(image!=null){
            System.out.println("已经获得上传文件!");
            File parentFile=new File(realPath);//指定文件保存路径
            if(!parentFile.exists()){//保存路径不存在,则创建
                parentFile.mkdirs();
            }
            for(int i=0;i<image.length;i++){
                File saveFile=new File(parentFile,imageFileName[i]);//parentFile:保存路径,imageFileName:保存文件名
                FileUtils.copyFile(image[i], saveFile);//将上传的image复制到saveFile中
            }
            ActionContext.getContext().put("message", "保存成功!");
        }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "tsuccess";
    }

    public File[] getImage() {
        return image;
    }

    public void setImage(File[] image) {
        this.image = image;
    }

    public String[] getImageFileName() {
        return imageFileName;
    }

    public void setImageFileName(String[] imageFileName) {
        this.imageFileName = imageFileName;
    }

    public String[] getImageContentType() {
        return imageContentType;
    }

    public void setImageContentType(String[] imageContentType) {
        this.imageContentType = imageContentType;
    }
}

 

第03步:配置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>
    <constant name="struts.action.extension" value="do,action"/>
    
    <package name="transform" namespace="/" extends="struts-default">
        <action name="list_*" class="com.self.action.FileUpload" method="{1}">
            <result name="tsuccess">
                /outdata.jsp
            </result>
        </action>
    </package>
</struts>

 

第04步:编写界面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<html>
  <body>
      <form method="post" action="list_uploadFile.action" enctype="multipart/form-data">
          文件1:<input type="file" name="image">
          文件2:<input type="file" name="image">
          文件3:<input type="file" name="image">
          <BR>
          <input type="submit" value="上传文件">
      </form>
  </body>
</html>

 

第05步:注意事项

注意事项请参考:struts文件上传(单文件)

第06步:导入包

技术分享

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。