Www.helper3.com
热门关键字: php net javascript java
Struts2基础 专题
专题目录
您的位置:java > Struts2基础专题 > Struts2上传多个文件例子
Struts2上传多个文件例子
作者:--    发布时间:2019-11-20 10:15:53

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

这里创建一个web工程:strut2uploadfiles,来演示在多个复选框如何设置的默认值,整个项目的结构如下图所示:

1. 动作类

在action类,可以使用列表或数组以存储上传的文件。

fileuploadaction.java

package com.h3.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>struts2上传多个文件示例</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>struts2上传多个文件示例</h1>

<div><div class="ads-in-post hide_if_width_less_800">
<script async  class="lazy" data-original="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 728x90 - after2ndh4 -->
<ins class="adsbygoogle hide_if_width_less_800" 
     
     data-ad-client="ca-pub-2836379775501347"
     data-ad-slot="3642936086"
	 data-ad-region="h3region"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div></div><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.h3.common.action.multiplefileuploadaction" 
            method="display">
		<result name="none">pages/multiplefileupload.jsp</result>
	</action>
		
	<action name="resultaction" 
	     class="com.h3.common.action.multiplefileuploadaction" 
             method="upload">
		 <result name="success">pages/result.jsp</result>
	</action>
   </package>
	
</struts>

4. 示例

http://localhost:8080/struts2uploafiles/multiplefileuploadaction.action

参考

  1. struts2文件上传示例
  2. struts2文件上传文档
  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

下载代码 - 


滚动到顶部 滚动到底部
网站声明:
本站部分内容来自网络,如您发现本站内容
侵害到您的利益,请联系本站管理员处理。
联系站长
QQ群:570078429
网站备案号:
粤ICP备17073857号