日期:2014-05-17 浏览次数:20819 次
<html>
<head>
<scriptlanguage="javascript">
functionaddComponent()
{
varuploadHTML=document.createElement("<inputtype='file' name='upload'/>");
document.getElementById("files").appendChild(uploadHTML);
uploadHTML=document.createElement("<p/>");
document.getElementById("files").appendChild(uploadHTML);
}
</script>
</head>
<body>
<inputtype="button"onclick="addComponent();"value="添加文件"/>
<br/>
<formonsubmit="returntrue;"action="/struts2/test/upload.action"
method="post"enctype="multipart/form-data">
<spanid="files"><inputtype='file'name='upload'/>
<p/>
</span>
<inputtype="submit"value="上传"/>
</form>
</body>
</html>
importjava.io.*;
importcom.opensymphony.xwork2.ActionSupport;
publicclassUploadMoreActionextendsActionSupport
{
privatejava.util.List<File>uploads;
privatejava.util.List<String>fileNames;
privatejava.util.List<String>uploadContentTypes;
publicjava.util.List<String>getUploadFileName()
{
returnfileNames;
}
publicvoidsetUploadFileName(java.util.List<String>fileNames)
{
this.fileNames=fileNames;
}
publicjava.util.List<File>getUpload()
{
returnuploads;
}
publicvoidsetUpload(java.util.List<File>uploads)
{
this.uploads=uploads;
}
publicvoidsetUploadContentType(java.util.List<String>contentTypes)
{
this.uploadContentTypes=contentTypes;
}
publicjava.util.List<String>getUploadContentType()
{
returnthis.uploadContentTypes;
}
publicStringexecute()throwsException
{
if(uploads!=null)
{
inti=0;
for(;i<uploads.size();i++)
{
java.io.InputStreamis=newjava.io.FileInputStream(uploads.get(i));
java.io.OutputStreamos=newjava.io.FileOutputStream(
"d:upload"+fileNames.get(i));
bytebuffer[]=newbyte[8192];
intcount=0;
while((count=is.read(buffer))>0)
{
os.write(buffer,0,count);
}
os.close();
is.close();
}
}
returnSUCCESS;
}
}