jsp文件
<%@ page language="java" pageEncoding="gbk"%>
<%@ 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" %>
<html:html lang="true">
<head>
<title>addfile.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">
<SCRIPT LANGUAGE="JavaScript">
<!--
var n = 1;
function add(){
if(n>5){
alert("不能在加了");
return;
}
var otable = document.getElementById("tablee");
var orow=otable.insertRow();
var ocol2 = orow.insertCell();
var ocol3 = orow.insertCell();
var ocol4 = orow.insertCell();
ocol2.innerHTML ="<input type=file name=files+"+n+">";
ocol3.innerHTML ="<input type='button' value='添加' onClick='add()' />";
ocol4.innerHTML ="<input type='button' value='删除' onClick='javascript:deleteRow();return false;' />";
n++;
}
function deleteRow(){
if(n<=1){
alert("不能在删了");
return;
}
var otable = document.getElementById("tablee");
// var rowIndex = event.srcElement.parentElement.parentElement.parentElement.rowIndex;
var rowIndex =event.srcElement.parentElement.parentElement.rowIndex
/*
event 事件
srcElement button
parentElement 上1层td
parentElement 上1层tr
rowIndex tr的rowIndex
*/
otable.deleteRow(rowIndex);
n--;
}
//-->
</SCRIPT>
</head>
<body>
<html:form action="/file.do?method=add" enctype="multipart/form-data">
<table id="tablee">
<tr>
<td>文件上传</td>
<td> 添加</td>
<td> 删除</td>
</tr>
<tr>
<td> <input type="file" name="files"> </td>
<td>
<input type="button" value="添加" onClick="add()" />
</td>
<td>
<input type="button" value="删除" onClick="deleteRow()" />
</td>
</tr>
</table>
<html:submit></html:submit>
</html:form>
</body>
</html:html>
action
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.hcwy.struts.action;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Hashtable;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.apache.struts.upload.FormFile;
import com.hcwy.struts.form.FileForm;
/**
* MyEclipse Struts
* Creation date: 05-09-2008
*
* XDoclet definition:
* @struts.action path="/file" name="fileForm" input="/form/file.jsp" parameter="method" scope="request" validate="true"
*/
public class FileAction extends DispatchAction {
private static long uuidCount = 0;
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
FileForm fileForm = (FileForm) form;
int i=0;
Hashtable files = fileForm.getMultipartRequestHandler().getFileElements();
for(Map.Entry<String,FormFile> entry : (Set<Map.Entry<String,FormFile>>)(files.entrySet())) {
FormFile ff=entry.getValue();
up(ff);
i++;
}
System.out.println(i);
return null;
}
//保存
public static void up(FormFile ff){
String dir1=getUUid();// + "."+ (ff.getFileName().split("\\.")[1]);
String dir ="C:/upload"+"/"+dir1;
try {
OutputStream out = new FileOutputStream(new File(dir));
byte b[] = ff.getFileData();
out.write(b);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//UUID
public static String getUUid() {
uuidCount++;
long time = System.currentTimeMillis();
String tmpStr="0000"+Long.toHexString(uuidCount);
String uuid = Long.toHexString(time) + tmpStr.substring(tmpStr.length()-4)
+ Long.toHexString(Double.doubleToLongBits(Math.random()));
uuid = uuid.substring(0, 24).toUpperCase();
return uuid;
}
}