1.JSP 代码
<%@ page language="java" pageEncoding="utf-8"%>
<html>
<head>
<title>struts upload by WJS</title>
<script language="javascript">
var rnum = 1;
var buttonId=0;
var b=0;
function addRow()
{
var oT = document.getElementById("oTable");
var newTR = oT.insertRow(oT.rows.length-1);
newTR.id = "row" + rnum;
var newTD0 = newTR.insertCell();
var newTD1 = newTR.insertCell();
newTD1.setAttribute("colSpan","2");
newTD0.innerText = "file";
newTD1.innerHTML = "<input name=/"files("
+ rnum
+ ")/" type=/"file/" > <input name=/"del/" type=/"button/" value=/" del /" onClick=/"deleteRow(" + rnum +");/">"
+"<input name=/"add"+buttonId+"/" id=/"add"+buttonId+"/" type=/"button/" value=/" 继续添加文件/" style=/"background-color: #FFF; border: 0px;/" οnclick=/"addRow();hiddenBt("+buttonId+");/">";
rnum++;
buttonId++;
}
function deleteRow(line)
{
oTable.deleteRow(document.getElementById("row" + line).rowIndex);
}
function hiddenTr()
{
document.getElementById("tr1").style.display = "none";
//document.all.test.style.display = "none";
}
function hiddenBt(id)
{
document.getElementById("add"+id).style.display="none";
//var a=document.getElementById("add"+id).value;
//alert(a);
}
</script>
</head>
<body>
<form name="updForm" method="post" action="../file.do?command=uploadFile" enctype="multipart/form-data">
<table id="oTable">
<tr>
<td>用户名</td>
<td><input name="userName" type="text"></td>
</tr>
<tr id="tr1">
<td>添加文件</td>
<td><input type="button" name="add" id="add" value=" + " style="background-color: #FFF; border: 0px;" οnclick="addRow();hiddenTr();"></td>
</tr>
<tr>
<td><input name="sub" type="submit" value=" sub "></td>
</tr>
</table>
</form>
</body>
</html>
2.ActionForm 代码
public class FileForm extends ActionForm {
private String userName;
private List<FormFile> files = new ArrayList<FormFile>();
public List<FormFile> getFiles() {
return files;
}
public void setFiles(List<FormFile> files) {
this.files = files;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}
3.Action代码
public class FileAction extends DispatchAction {
public ActionForward uploadFile(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
MultipartRequestHandler multipartRequestHandler=form.getMultipartRequestHandler();
Hashtable elements=multipartRequestHandler.getFileElements();
Collection<FormFile> values=elements.values();//Collection DEBUG不下去,可能是没加入Collection<FormFile>或者下面出错
//获取JSP穿过来的客户名,为定位客户所有的上传的文件,方便日后索引
FileForm getUserName=(FileForm)form;
String fname=getUserName.getUserName();
//文件保存到的文件夹
String filePath=servlet.getServletContext().getRealPath("/files");
String imagePath=servlet.getServletContext().getRealPath("/img");
File img=new File(imagePath);
File files=new File(filePath);
if(!img.exists())
img.mkdir();
if(!files.exists())
files.mkdir();
int k=0;
for(Iterator i=values.iterator();i.hasNext();)
{
FormFile file=(FormFile)i.next();
if(""!=file.getFileName())
{
FileOutputStream fos=null;
//用于保存图片
if(file.getFileName().toLowerCase().endsWith(".jpg")||file.getFileName().toLowerCase().endsWith(".bmp")||file.getFileName().toLowerCase().endsWith(".gif")||file.getFileName().toLowerCase().endsWith(".png"))
{
fos=new FileOutputStream(imagePath+"/"+fname+getStringDate()+"-"+k+".gif");
}else
{
//获取文件名点的位置,从而获得上传文件的文件名
int lastPoint=file.getFileName().lastIndexOf(".");
String name=file.getFileName().substring(lastPoint, file.getFileName().length());
fos=new FileOutputStream(filePath+"/"+fname+getStringDate()+"-"+k+name);
}
fos.write(file.getFileData());
fos.flush();
fos.close();
}
k++;
}
return mapping.findForward("uploadFile_success");
}
4. <!-- 限制STRUTS上传文件大小 -->
<controller maxFileSize="10M"></controller>