网上找的代码不知道为啥都不能用,自己写一个简陋点的。
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>