Struts 1.2 批量上传。

本文介绍了一个基于Struts框架实现的文件上传功能示例,包括struts-config.xml配置文件、ActionForm与Action的具体实现及JSP页面的编写。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网上找的代码不知道为啥都不能用,自己写一个简陋点的。 

struts-config.xml:

<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">



<struts-config>   <data-sources />   <form-beans >     <form-bean name="ttForm" type="action.TtForm" />



</form-beans>



  <global-exceptions />   <global-forwards />   <action-mappings >     <action path="/tt" scope="request" name="ttForm" type="action.TtAction" />



  </action-mappings>



  <message-resources parameter="ApplicationResources" /> </struts-config>

 

 

ActionForm:

/*

 * Generated by MyEclipse Struts

 * Template path: templates/java/JavaClass.vtl

 */

package action;



import java.util.HashMap;

import java.util.Map;



import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;

import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionMapping;

import org.apache.struts.upload.FormFile;

import org.apache.struts.validator.LazyValidatorForm;



/** 

 * MyEclipse Struts

 * Creation date: 06-21-2008

 * 

 * XDoclet definition:

 * @struts.form name="ttForm"

 */

public class TtForm extends ActionForm {

	/*

	 * Generated Methods

	 */



	private Map<String, FormFile> map = new HashMap<String, FormFile>();

	/** 

	 * Method validate

	 * @param mapping

	 * @param request

	 * @return ActionErrors

	 */

	public ActionErrors validate(ActionMapping mapping,

			HttpServletRequest request) {

		// TODO Auto-generated method stub

		return null;

	}

	

	public void setFile(String key,FormFile f) {

		if (f != null && f.getFileSize() > 0) {

			this.map.put(key, f);

		}

	}

	

	public FormFile getFile(String key) {

		return this.map.get(key);

	}



	/** 

	 * Method reset

	 * @param mapping

	 * @param request

	 */

	public void reset(ActionMapping mapping, HttpServletRequest request) {

		// TODO Auto-generated method stub

	}



	public Map<String, FormFile> getMap() {

		return map;

	}



	public void setMap(Map<String, FormFile> map) {

		this.map = map;

	}

}

 Action:

	public ActionForward execute(ActionMapping mapping, ActionForm form,

			HttpServletRequest request, HttpServletResponse response) throws Exception {

		// TODO Auto-generated method stub

		TtForm f = (TtForm)form;

		response.setCharacterEncoding("utf-8");

		PrintWriter out = response.getWriter();

		

		Set<Map.Entry<String, FormFile>> s = f.getMap().entrySet();

		out.println("共上传:"+s.size()+"个文件<br/>");

		for (Map.Entry<String, FormFile> mm : s) {

			FormFile ff = mm.getValue();

			out.println("FileName:"+ff.getFileName()+"<br/>");

			out.println("FileSize:"+ff.getFileSize()+"<br/>");

			

			out.println("=============<br/>");

		}

		return null;

	}

JSP:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

  <head>



    <title>My JSP 'upload.jsp' starting page</title>

    

	<meta http-equiv="pragma" content="no-cache">

	<meta http-equiv="cache-control" content="no-cache">

	<meta http-equiv="expires" content="0">    

	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

	<meta http-equiv="description" content="This is my page">

	<!--

	<link rel="stylesheet" type="text/css" href="styles.css">

	-->

<script type="text/javascript">

	var index = 2;

	function add() {

		var form = document.forms[0];

		var input = document.createElement("input");

		input.type = "file";

		input.name = "file(file"+index+")";

		form.insertBefore(input,form.elements[form.elements.length - 1]);

		form.insertBefore(document.createElement("br"),form.elements[form.elements.length - 1]);

		index ++;

		

	}

</script>

  </head>

  

  <body>

  <input type="button" value="添加上传文件" onclick="add()"/>

 	<html:form action="/tt" method="post" enctype="multipart/form-data">

 	<html:file property="file(file1)"></html:file>

 	<br/>

 	<html:submit></html:submit>

 	</html:form>

  </body>

</html>

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值