为什么多文件上传只能上传成功第一个文件??

dhaigang 2009-02-07
下面是upload.jsp

<%@ page language="java" contentType="text/html; charset=GBK"%>
<%@taglib prefix="s" uri="/struts-tags"%>
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=GBK" />
		<title>使用List上传多个文件</title>
		<SCRIPT type="text/javascript">
function addComponent()
{
  var uploadHTML=document.createElement("<input type='file' name='upload'/>");
  document.getElementById("fi").appendChild(uploadHTML);
   uploadHTML=document.createElement("<p/>");
  document.getElementById("fi").appendChild(uploadHTML);
}
</SCRIPT>
	</head>
	<body>
		<s:fielderror />
		<form onsubmit="return true;" action="upload.action" method="post"
			enctype="multipart/form-data">
			<input type="button" onclick="addComponent();" value="添加文件" />
			<span id="fi"> <input type='file' name='upload' />
				<p />
			</span>
			<input type="submit" value="上传" />
		</form>
	</body>
</html>

下面是UploadAction.java
package lee;

import com.opensymphony.xwork2.Action;
import org.apache.struts2.ServletActionContext;
import java.util.*;
import java.io.*;


import com.opensymphony.xwork2.ActionSupport;



public class UploadAction extends ActionSupport
{
	private String title;
    private List<File> upload;
    private List<String> uploadContentType;
    private List<String> uploadFileName;


    private String savePath;
	
    public void setSavePath(String value)
	{
        this.savePath = value;
    }

    private String getSavePath() throws Exception 
	{
        return ServletActionContext.getRequest().getRealPath(savePath);
    }
	
	public void setTitle(String title) {
		this.title = title; 
	}

	public void setUpload(List<File> upload) {
		this.upload = upload; 
	}

	public void setUploadContentType(List<String> uploadContentType) {
		this.uploadContentType = uploadContentType; 
	}

	public void setUploadFileName(List<String> uploadFileName) {
		this.uploadFileName = uploadFileName; 
	}

	public String getTitle() {
		return (this.title); 
	}

	public List<File> getUpload() {
		return (this.upload); 
	}

	public List<String> getUploadContentType() {
		return (this.uploadContentType); 
	}

	public List<String> getUploadFileName() {
		return (this.uploadFileName); 
	}
	@Override
    public String execute() throws Exception
	{
		List<File> files = getUpload();
		for (int i = 0 ; i < files.size() ; i++)
		{
			
			FileOutputStream fos = new FileOutputStream(getSavePath() + "\\" + getUploadFileName().get(i));
			FileInputStream fis = new FileInputStream(files.get(i));
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0)
			{
				fos.write(buffer , 0 , len);
			}
		}
        return SUCCESS;
    }
}

下面是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.custom.i18n.resources" value="globalMessages"/>
	<constant name="struts.i18n.encoding" value="GBK"/>
	<package name="upload" extends="struts-default">
	
		<action name="upload" class="lee.UploadAction">
            <interceptor-ref name="fileUpload"> 
                <param name="allowedTypes">image/bmp,image/png,image/gif,image/jpeg,image/pjpeg</param> 
            </interceptor-ref> 
            <interceptor-ref name="defaultStack"/>    
            <param name="savePath">/upload</param>
			<result name="input">/upload.jsp</result>	
			<result>/succ.jsp</result>	
		</action>
		
	</package>
</struts>	

下面是web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" 
	xmlns="http://java.sun.com/xml/ns/j2ee" 
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

    <filter>
        <filter-name>struts</filter-name>
        <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>struts</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


    <filter> 
        <filter-name> struts-cleanup </filter-name> 
        <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class> 
    </filter>
    <filter-mapping>
        <filter-name>struts-cleanup</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>


</web-app>

可是只能上传成功第一个文件,为什么?请指教
zhengyu 2009-02-07
把upLoad属性类型改成File[]试试
kaig 2009-03-01
uploadFileName没有赋值怎么能够获取到?
an9elkiss 2009-05-06
不是STRUTS的问题,
是JS不太规范,
改成下面这样就行了
function addComponent()
{
  var uploadHTML=document.createElement("input");
  uploadHTML.name="fileList";
  uploadHTML.type="file";
  document.getElementById("fi").appendChild(uploadHTML);
   uploadHTML=document.createElement("br");
  document.getElementById("fi").appendChild(uploadHTML);
}
wenxiang_tune 2009-05-13
action中属性问题 应该采用数组来接收upload!
Global site tag (gtag.js) - Google Analytics