Struts2教程:上传任意多个文件

开发 后端
本文为Struts2教程,本部分教你如何上传任意多个文件。Struts2虽然在大版本号上是第二个版本,但基本上在配置和使用上已经完全颠覆了Struts1.x的方式。

一、上传单个文件

上传文件是很多Web程序都具有的功能。在Struts1.x中已经提供了用于上传文件的组件。而在Struts2中提供了一个更为容易操作的上传文件组件。所不同的是,Struts1.x的上传组件需要一个ActionForm来传递文件,而Struts2的上传组件是一个拦截器(这个拦截器不用配置,是自动装载的)。在本文中先介绍一下如何用struts2上传单个文件,***介绍一下用struts2上传任意多个文件。

要用Struts2实现上传单个文件的功能非常容易实现,只要使用普通的Action即可。但为了获得一些上传文件的信息,如上传文件名、上传文件类型以及上传文件的Stream对象,就需要按着一定规则来为Action类增加一些getter和setter方法。

在Struts2中,用于获得和设置java.io.File对象(Struts2将文件上传到临时路径,并使用java.io.File打开这个临时文件)的方法是getUpload和setUpload。获得和设置文件名的方法是getUploadFileName和setUploadFileName,获得和设置上传文件内容类型的方法是getUploadContentType和setUploadContentType。下面是用于上传的动作类的完整代码:

package action;  
 
import java.io.*;  
import com.opensymphony.xwork2.ActionSupport;  
 
public class UploadAction extends ActionSupport   
{  
    private File upload;  
    private String fileName;  
    private String uploadContentType;  
      
    public String getUploadFileName()  
    {  
        return fileName;  
    }  
 
    public void setUploadFileName(String fileName)  
    {  
        this.fileName = fileName;  
    }  
 
    public File getUpload()  
    {  
        return upload;  
    }  
 
    public void setUpload(File upload)  
    {  
        this.upload = upload;  
    }  
    public void setUploadContentType(String contentType)  
    {  
        this.uploadContentType=contentType;  
      
    }  
      
    public String getUploadContentType()  
    {  
        return this.uploadContentType;  
    }  
    public String execute() throws Exception  
    {     
        java.io.InputStream is = new java.io.FileInputStream(upload);  
        java.io.OutputStream os = new java.io.FileOutputStream("d:\\upload\\" + fileName);  
        byte buffer[] = new byte[8192];  
        int count = 0;  
        while((count = is.read(buffer)) > 0)  
        {  
            os.write(buffer, 0, count);  
        }  
        os.close();  
        is.close();  
        return SUCCESS;  
    }  
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.

在execute方法中的实现代码就很简单了,只是从临时文件复制到指定的路径(在这里是d:\upload)中。上传文件的临时目录的默认值是javax.servlet.context.tempdir的值,但可以通过struts.properties(和struts.xml在同一个目录下)的struts.multipart.saveDir属性设置。Struts2上传文件的默认大小限制是2M(2097152字节),也可以通过struts.properties文件中的struts.multipart.maxSize修改,如struts.multipart.maxSize=2048 表示一次上传文件的总大小不能超过2K字节。

下面的代码是上传文件的JSP页面代码:

 
< %@ page language="java" import="java.util.*" pageEncoding="GBK"%> 
< %@ taglib prefix="s" uri="/struts-tags"%> 
 
< html> 
    < head> 
        < title>上传单个文件< /title> 
    < /head> 
 
    < body> 
        < s:form action="upload" namespace="/test" 
            enctype="multipart/form-data"> 
            < s:file name="upload" label="输入要上传的文件名" /> 
            < s:submit value="上传" /> 
        < /s:form> 
 
    < /body> 
< /html> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

也可以在success.jsp页中通过< s:property>获得文件的属性(文件名和文件内容类型),代码如下:

< s:property value="uploadFileName"/>

二、上传任意多个文件

在Struts2中,上传任意多个文件也非常容易实现。首先,要想上传任意多个文件,需要在客户端使用DOM技术生成任意多个< input type=”file” />标签。name属性值都相同。代码如下:

< html> 
    < head> 
        < script language="javascript"> 
 
function addComponent()  
{  
        var uploadHTML = document.createElement( "< input type='file'  name='upload'/>");  
        document.getElementById("files").appendChild(uploadHTML);  
        uploadHTML = document.createElement( "< p/>");  
        document.getElementById("files").appendChild(uploadHTML);  
}  
< /script> 
    < /head> 
    < body> 
        < input type="button" onclick="addComponent();" value="添加文件" /> 
        < br /> 
        < form onsubmit="return true;" action="/struts2/test/upload.action" 
            method="post" enctype="multipart/form-data"> 
            < span id="files"> < input type='file' name='upload' /> 
                < p /> 
            < /span> 
            < input type="submit" value="上传" /> 
        < /form> 
    < /body> 
 
< /html> 
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

上面的javascript代码可以生成任意多个< input type=’file’>标签,name的值都为file(要注意的是,上面的javascript代码只适合于IE浏览器,firefox等其他浏览器需要使用他的代码)。至于Action类,和上传单个文件的Action类基本一至,只需要将三个属性的类型改为List即可。代码如下:

package action;  
 
import java.io.*;  
import com.opensymphony.xwork2.ActionSupport;  
 
public class UploadMoreAction extends ActionSupport  
{  
    private java.util.List< File> uploads;  
    private java.util.List< String> fileNames;  
    private java.util.List< String> uploadContentTypes;  
 
    public java.util.List< String> getUploadFileName()  
    {  
        return fileNames;  
    }  
    public void setUploadFileName(java.util.List< String> fileNames)  
    {  
        this.fileNames = fileNames;  
    }  
    public java.util.List< File> getUpload()  
    {  
        return uploads;  
    }  
 
    public void setUpload(java.util.List< File> uploads)  
    {  
        this.uploads = uploads;  
    }  
 
    public void setUploadContentType(java.util.List< String> contentTypes)  
    {  
        this.uploadContentTypes = contentTypes;  
 
    }  
 
    public java.util.List< String> getUploadContentType()  
    {  
        return this.uploadContentTypes;  
    }  
 
    public String execute() throws Exception  
    {  
        if (uploads != null)  
        {  
            int i = 0;  
            for (; i <  uploads.size(); i++)  
            {  
                java.io.InputStream is = new java.io.FileInputStream(uploads.get(i));  
                java.io.OutputStream os = new java.io.FileOutputStream(  
                        "d:\\upload\\" + fileNames.get(i));  
                byte buffer[] = new byte[8192];  
                int count = 0;  
                while ((count = is.read(buffer)) > 0)  
                {  
                    os.write(buffer, 0, count);  
                }  
                os.close();  
                is.close();  
            }  
        }  
        return SUCCESS;  
    }  
}  
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.

在execute方法中,只是对List对象进行枚举,在循环中的代码和上传单个文件时的代码基本相同。如果读者使用过struts1.x的上传组件,是不是感觉Struts2的上传功能更容易实现呢?在Struts1.x中上传多个文件时,可是需要建立带索引的属性的。而在Struts2中,就是这么简单就搞定了。图1是上传任意多个文件的界面。

上传任意多个文件的界面


图1

【编辑推荐】

  1. Struts2教程:在Action类中获得HttpServletResponse对象
  2. Struts2教程:使用Validation框架验证数据
  3. Struts2教程:使用validate方法验证数据
  4. Struts2教程:struts.xml常用配置解析
  5. Struts2教程:处理一个form多个submit
责任编辑:yangsai 来源: BlogJava
相关推荐

2009-02-04 14:00:59

2009-06-04 09:41:50

struts2上传文件

2009-06-25 15:11:28

Struts2教程Struts2程序

2009-02-04 11:00:44

2009-06-08 16:44:00

Struts2文件上传

2009-06-25 15:22:03

Struts2教程一个form多个sub

2009-02-04 10:51:07

2009-06-03 14:19:34

Struts2Guice

2009-06-25 16:04:30

2009-06-25 15:26:25

Struts2教程struts.xml常

2009-06-04 08:34:24

Struts2配置struts.xml

2009-02-04 15:04:13

2009-06-25 15:54:42

Struts2教程拦截器

2009-02-04 11:37:15

2009-07-29 09:54:34

struts2和str

2009-06-25 15:37:12

Struts2教程Validation框

2009-02-04 14:19:38

2009-06-25 15:33:12

Struts2教程使用validate验证数据

2009-06-04 08:53:57

struts2视频教程

2009-02-04 13:13:03

点赞
收藏

51CTO技术栈公众号