我想上传多个附件,并且给每个附件添加标题,但是上传多个文件后在struts中输出的附件名和标题老是不对应,这是为什么,请高手帮忙啊
这是我的代码
<%@ page language="java" pageEncoding="gb2312"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>UploadFile.jsp</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">
--> </head>
<%response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
%>
<style type="text/css"> .file { font: 10pt; color: black; } </style>
<script language="javascript">
var i=0;
var n="234";
function addFile() {
i++;
currRow=conditionTable.insertRow();
cellc=currRow.insertCell();
cellcContext= "附件名:<input type='text' name='file("+(i-1)+")'> <input type='file' NAME='file("+(i-1)+")'> <button onclick='removeFile();'>删除</button><br>";
cellc.innerHTML=cellcContext;
//alert(cellcContext);
}
function findTD(o){
if (o.nodeName=="TR"||o.nodeName=="TABLE") return;
if(o.nodeName=="TD")
return (o);
else
return (o.parentElement);
}
function removeFile(){
o = findTD(event.srcElement);
//alert(o.parentElement.rowIndex*1);
conditionTable.deleteRow(o.parentElement.rowIndex*1);
} function uploadFile(){
document.form1.action="../accessories.do?method=addAccess";
document.form1.submit();
}
</script>
<body>
<form enctype="multipart/form-data" name="form1" method="post" >
<table align="center">
<tr>
<td>
<table id=conditionTable border=1 align="center">
</table>
</td>
</tr>
<tr>
<td>
<a href="javascript:void(addFile());" class="file">添加附件</a> <a href="javascript:void(uploadFile());" class="file">上传附件</a>
</td>
<td><%
String wrong=(String)request.getAttribute("wrong");
if(wrong!=null)
{
out.print(wrong);
wrong="";
}
%></td>
</tr>
</table>
</form>
</body>
</html:html>
form
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.edu.struts.form;import java.util.ArrayList;
import java.util.List;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;/**
* MyEclipse Struts
* Creation date: 11-07-2008
*
* XDoclet definition:
* @struts.form name="accessoriesForm"
*/
public class AccessoriesForm extends ActionForm {
/*
* Generated Methods
*/
private List files = new ArrayList();
public FormFile getFile(String index) {
return (FormFile) files.get(Integer.parseInt(index));
} public void setFile(String index, FormFile file) {
this.files.add(file);
} public List getFiles() {
return this.files;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
} /**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}}
Action
public ActionForward addAccess(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
AccessoriesForm accessoriesForm = (AccessoriesForm) form;// TODO Auto-generated method stub
List files = accessoriesForm.getFiles();
for (int i = 0; i < files.size(); i++) {
// for(int i=files.size()-1;i>=0;i--){
FormFile file = (FormFile) files.get(i);
time=String.valueOf(da.getTime());
String name2=request.getParameter("file("+i+")");
System.out.print(name2);
System.out.println(file.getFileName());}
}
这是我的代码
<%@ page language="java" pageEncoding="gb2312"%><%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
<head>
<html:base />
<title>UploadFile.jsp</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">
--> </head>
<%response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
%>
<style type="text/css"> .file { font: 10pt; color: black; } </style>
<script language="javascript">
var i=0;
var n="234";
function addFile() {
i++;
currRow=conditionTable.insertRow();
cellc=currRow.insertCell();
cellcContext= "附件名:<input type='text' name='file("+(i-1)+")'> <input type='file' NAME='file("+(i-1)+")'> <button onclick='removeFile();'>删除</button><br>";
cellc.innerHTML=cellcContext;
//alert(cellcContext);
}
function findTD(o){
if (o.nodeName=="TR"||o.nodeName=="TABLE") return;
if(o.nodeName=="TD")
return (o);
else
return (o.parentElement);
}
function removeFile(){
o = findTD(event.srcElement);
//alert(o.parentElement.rowIndex*1);
conditionTable.deleteRow(o.parentElement.rowIndex*1);
} function uploadFile(){
document.form1.action="../accessories.do?method=addAccess";
document.form1.submit();
}
</script>
<body>
<form enctype="multipart/form-data" name="form1" method="post" >
<table align="center">
<tr>
<td>
<table id=conditionTable border=1 align="center">
</table>
</td>
</tr>
<tr>
<td>
<a href="javascript:void(addFile());" class="file">添加附件</a> <a href="javascript:void(uploadFile());" class="file">上传附件</a>
</td>
<td><%
String wrong=(String)request.getAttribute("wrong");
if(wrong!=null)
{
out.print(wrong);
wrong="";
}
%></td>
</tr>
</table>
</form>
</body>
</html:html>
form
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.edu.struts.form;import java.util.ArrayList;
import java.util.List;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;/**
* MyEclipse Struts
* Creation date: 11-07-2008
*
* XDoclet definition:
* @struts.form name="accessoriesForm"
*/
public class AccessoriesForm extends ActionForm {
/*
* Generated Methods
*/
private List files = new ArrayList();
public FormFile getFile(String index) {
return (FormFile) files.get(Integer.parseInt(index));
} public void setFile(String index, FormFile file) {
this.files.add(file);
} public List getFiles() {
return this.files;
}
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request) {
// TODO Auto-generated method stub
return null;
} /**
* Method reset
* @param mapping
* @param request
*/
public void reset(ActionMapping mapping, HttpServletRequest request) {
// TODO Auto-generated method stub
}}
Action
public ActionForward addAccess(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
AccessoriesForm accessoriesForm = (AccessoriesForm) form;// TODO Auto-generated method stub
List files = accessoriesForm.getFiles();
for (int i = 0; i < files.size(); i++) {
// for(int i=files.size()-1;i>=0;i--){
FormFile file = (FormFile) files.get(i);
time=String.valueOf(da.getTime());
String name2=request.getParameter("file("+i+")");
System.out.print(name2);
System.out.println(file.getFileName());}
}
<input type="file" name="files[2]">
<input type="file" name="files[3]">
<input type="text" name="texts[1]">
<input type="text" name="texts[2]">
<input type="text" name="texts[3]">private FormFile[] files;
private String[] texts;