Struts 2上传多个文件的示例

下载它– Struts2-Multiple-Files-Upload-Example.zip

在最后一个Struts 2文件上传示例中 ,允许用户选择一个文件并上传到服务器。 在本教程中,您将学习如何允许用户将多个文件上传到服务器。

1.动作课

在Action类中,可以使用List或Array来存储上载的文件。

FileUploadAction.java

package com.mkyong.common.action;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.opensymphony.xwork2.ActionSupport;

public class MultipleFileUploadAction extends ActionSupport{

	private List<File> fileUpload = new ArrayList<File>();
	private List<String> fileUploadContentType = new ArrayList<String>();
	private List<String> fileUploadFileName = new ArrayList<String>();

	public List<File> getFileUpload() {
		return fileUpload;
	}

	public void setFileUpload(List<File> fileUpload) {
		this.fileUpload = fileUpload;
	}

	public List<String> getFileUploadContentType() {
		return fileUploadContentType;
	}

	public void setFileUploadContentType(List<String> fileUploadContentType) {
		this.fileUploadContentType = fileUploadContentType;
	}

	public List<String> getFileUploadFileName() {
		return fileUploadFileName;
	}

	public void setFileUploadFileName(List<String> fileUploadFileName) {
		this.fileUploadFileName = fileUploadFileName;
	}
	
	public String upload() throws Exception{
		
	    for (File file: fileUpload) {
	        System.out.println("File :" + file);
	    }
	    
	    for (String fileName: fileUploadFileName) {
	        System.out.println("Filename : " + fileName);
	    }

	    for (String fileContentType: fileUploadContentType) {
	        System.out.println("File type : " + fileContentType);
	    }
	    
	    return SUCCESS;
		
	}

	public String display() {
		return NONE;
	}
	
}

2.结果页

使用<s:file>标记呈现多个文件上载组件,并将表单enctype类型设置为“ multipart / form-data”

fileupload.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
<s:head />
</head>

<body>
<h1>Struts 2 upload multiple files example</h1>

<s:form action="resultAction" namespace="/" 
method="POST" enctype="multipart/form-data">

<s:file label="File 1" name="fileUpload" size="40" />
<s:file label="File 2" name="fileUpload" size="40" />
<s:file label="FIle 2" name="fileUpload" size="40" />

<s:submit value="submit" name="submit" />
	
</s:form>

</body>
</html>

result.jsp

<%@ taglib prefix="s" uri="/struts-tags" %>
<html>

<body>
<h1>Struts 2 upload multiple files example</h1>

<h2>
   File Name : <s:property value="fileUploadFileName"/> 
</h2> 

<h2>
   Content Type : <s:property value="fileUploadContentType"/> 
</h2> 

<h2>
   File : <s:property value="fileUpload"/> 
</h2> 

</body>
</html>

3. 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.devMode" value="true" />

	<package name="default" namespace="/" extends="struts-default">
		
	<action name="multipleFileUploadAction" 
	    class="com.mkyong.common.action.MultipleFileUploadAction" 
            method="display">
		<result name="none">pages/multiplefileupload.jsp</result>
	</action>
		
	<action name="resultAction" 
	     class="com.mkyong.common.action.MultipleFileUploadAction" 
             method="upload">
		 <result name="success">pages/result.jsp</result>
	</action>
   </package>
	
</struts>

4.演示

http:// localhost:8080 / Struts2Example / multipleFileUploadAction.action

Struts 2 Multiple files upload example
Struts2 Multiple files upload example

参考

  1. Struts 2文件上传示例
  2. Struts 2文件文档
  3. http://struts.apache.org/2.0.14/docs/file-upload.html
  4. http://struts.apache.org/2.0.14/docs/how-do-we-upload-files.html
  5. http://www.mkyong.com/struts/struts-file-upload-example/

翻译自: https://mkyong.com/struts2/struts-2-upload-multiple-files-example/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值