11.struts2文件上传

文件上传
1.上传单个文件
2.上传多个文件
 
1.上传单个文件
实现步骤:
(1)导入一个Jar包:commons-io-1.3.2.jar。只所以要导入这个Jar包,是因为要用到一个工具类FileUtil。若不使用此工具类,就无需导入此包了。
(2)把form表单的enctype设置为:“multipart/form-data”,method设置为“post”,否则此表单不能用于上传。
如下:
<form enctype="multipart/form-data" action="xxx.action" method="post">
      <input  type="file" name="uf">
</form> 
(3)在Action类中添加以下属性
  private File uf;//上传的文件
  private String ufFileName;//文件名称 
           注意:蓝色部分对应于表单中文件字段的名称。而FileName是必须的。
最后是,在Action方法中实现对上传文件的操作。

2.上传多个文件

    与上传单个文件相比,发生了如下几个变化:
    (1)提交表单中出现多个文件上传栏,这多个的name属性名必须完全相同。
    (2)Action中文件不再为File类型了,而是File类型的数组或List。当然,文件名也为相应的数组或List了。
    (3)Action方法需遍历这些数组来上传这些文件。

 下面例子都已导入struts2核心jar包基础上,又导入了commons-io-1.3.2.jar包。
实例1:上传单个文件—fileupload
代码文档目录如下:

Step1:编写index.jsp页面
<%@ page pageEncoding="utf-8"%>

<html>
  <head>
    
    <title>upload page</title>

  </head>
  
  <body>
       <form action="single.action" enctype="multipart/form-data" method="post">
       
       文件<input type="file" name="uf"/><br/>
       <input type="submit" value="上传"/>
       </form>
  </body>
</html>

Step2:编写SingleFileUploadAction.java

package actions;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionContext;

public class SingleAction {
    private File uf;
    private String ufFileName;
    public File getUf() {
        return uf;
    }
    public void setUf(File uf) {
        this.uf = uf;
    }
    public String getUfFileName() {
        return ufFileName;
    }
    public void setUfFileName(String ufFileName) {
        this.ufFileName = ufFileName;
    }
    
    public String execute(){
        String savePath="D:/";
        if(uf!=null)
        {
            File saveFile=new File(savePath,ufFileName);
            try {
                //将 uf 文件的内容复制到saveFile中。
                FileUtils.copyFile(uf, saveFile);
                ActionContext.getContext().put("message","文件上传成功!");
            } catch (IOException e) {
                e.printStackTrace();
                ActionContext.getContext().put("message","文件上传失败!");
            }
            
        }else{
            ActionContext.getContext().put("message","没有指定要上传的文件");
            
        }
        System.out.println(ufFileName);
        return "success";
    }
    

}
Step2:编写web.xml与struts.xml
web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.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>

修改上传文件大小限制:默认最大2M
使用方法:
    在struts.xml中添加下面这段代码:

   <constant name="struts.multipart.maxSize" value="5242880"/>

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.multipart.maxSize" value="9999242880"></constant>
     <package name="one" extends="struts-default">
        <action name="single" class="actions.SingleAction">
            <result>/message.jsp</result>
        </action>
    
    </package>
    
</struts>

 Step3:编写message.jsp

<%@ page pageEncoding="utf-8" isELIgnored="false"%>

<html>
  <head>
    
    <title>message page</title>
    
    

  </head>
  
  <body>
  提示信息:${message}

  
</body>
</html>
注意,当文件内容为空时,tomcat后台会报错。
部署发布,启动tomcat,输入地址:
http://127.0.0.1:8080/single_file_upload/
 

实例2:上传多个文件—fileupload
Step1:编写index.jsp页面
<%@ page pageEncoding="utf-8"%>

<html>
  <head>
    
    <title>upload page</title>

  </head>
  
  <body>
       <form action="multiple.action" enctype="multipart/form-data" method="post">
       
       文件1<input type="file" name="ufs"/><br/>
       文件2<input type="file" name="ufs"/><br/>
       文件3<input type="file" name="ufs"/><br/>
       <input type="submit" value="上传"/>
       </form>
  </body>
</html>

Step2:编写MultipleAction.java

package actions;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;

import com.opensymphony.xwork2.ActionContext;

public class MultipleAction {
    private File[] ufs;
    private String[] ufsFileName;

    public File[] getUfs() {
        return ufs;
    }

    public void setUfs(File[] ufs) {
        this.ufs = ufs;
    }

    public String[] getUfsFileName() {
        return ufsFileName;
    }

    public void setUfsFileName(String[] ufsFileName) {
        this.ufsFileName = ufsFileName;
    }

    public String execute() {
        StringBuffer sb=new StringBuffer();
        String savePath = "D:/";
        if (ufs != null) {
            for (int i = 0; i < ufs.length; i++) {
                if (ufs[i] != null) {
                    File saveFile = new File(savePath,ufsFileName[i]);
                    try {
                        FileUtils.copyFile(ufs[i], saveFile);
                        sb.append("文件"+(i+1)+"上传成功!");
                    } catch (IOException e) {
                        e.printStackTrace();
                        sb.append("文件"+(i+1)+"上传失败!");
                    }
                } 
                
            }
        }else {
            sb.append("文件指定要上传的文件!");
        
        }
        ActionContext.getContext().put("message",sb.toString());
        return "success";
    }

}

 Step2:编写web.xml与struts.xml

web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.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>

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>
    <package name="one" extends="struts-default">
        <action name="multiple" class="actions.MultipleAction">
            <result>/message.jsp</result>
        </action>
    
    </package>
    
</struts>

Step3:编写message.jsp

<%@ page pageEncoding="utf-8" isELIgnored="false"%>

<html>
  <head>
    <title>message page</title>
  </head>
  
  <body>
  提示信息:${message}
     
        
  </body>
</html>

 部署发布,启动tomcat,输入地址:

http://127.0.0.1:8080/mutiple_fle_upload/

注意:1.txt有内容,2.txt有内容,3.txt内容为空。

原文地址:https://www.cnblogs.com/xingyunblog/p/4018603.html