在前面的《Apache Commons FileUpload實現多文件上傳》文章中介紹了使用第三方組件ApacheCommons FileUpload來實現多文件的上傳
[html]
<span ><%@ page language=
<%@ taglib uri=
<!DOCTYPE HTML PUBLIC
<html>
<head>
<title>multiUploadDemo</title>
<script type=
</script>
</head>
<body bgcolor=
<div id=
<form method=
<table id=
<input type=
<input type=
</form>
</body>
</html>
</span>
在前台頁面中引用的js文件中的代碼如下
[javascript]
<span > var num =
function upload(){
document
}
function additem(id)
{
var row
row = eval(
if(row != null )
{
cell = row
str=
cell
}
num++;
}
function deleteitem(obj
{
var rowNum
curRow = obj
rowNum = eval(
eval(
}
function callback(msg)
{
document
}</span>
UploadActionForm
[java]
<span >package myupload;
import java
import java
import org
import org
public class UploadActionForm extends ActionForm {
private List myFiles;
public UploadActionForm() {
myFiles = new ArrayList();
}
public List getMyFiles() {
return myFiles;
}
// 注意這個方法的定義 不加中間的循環是會出錯的
public UploadFile getUploadFile(int index) {
int size = myFiles
if (index > size
for (int i =
myFiles
}
}
return (UploadFile) myFiles
}
public void setMyFiles(List myFiles) {
this
}
}
</span>
UploadFile
[java]
<span >package myupload;
import java
import org
public class UploadFile implements Serializable {
private FormFile file;
public FormFile getFile() {
System
return file;
}
public void setFile(FormFile file) {
this
}
}
</span>
UploadTestAction
[java]
<span >package myupload;
import java
import java
import java
import java
import javax
import javax
import javax
import org
import org
import org
import org
import org
import llections
public class UploadTestAction extends Action {
public ActionForward execute(ActionMapping mapping
HttpServletRequest request
throws IOException
UploadActionForm multiUploadForm = (UploadActionForm) form;
ArrayList myFiles = (ArrayList)multiUploadForm
String fileStr =
System
for (int i =
UploadFile uploadFile = (UploadFile) myFiles
FormFile file = uploadFile
if (file == null) {
System
} else {
FileOutputStream fos = new FileOutputStream(
fos
fos
fos
}
}
return mapping
}
}</span>
最後就是我們的struts
[html]
<span ><?xml version=
<!DOCTYPE struts
<struts
<form
<form
</form
<action
<action path=
type=
name=
scope=
>
<forward name=
</action>
</action
<controller maxFileSize=
</struts
該文件中配置了jsp頁面提交後交由哪個Action進行處理的具體信息
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28178.html