JSP页面如下:
<script language="javascript" >
function addFile(){
var Rows=tableFiles.rows;//类似数组的Rows
var newRow=tableFiles.insertRow(tableFiles.rows.length);//插入新的一行
var Cells=newRow.cells;//类似数组的Cells
var newCell=Rows(newRow.rowIndex).insertCell(Cells.length);
newCell.align="center";
newCell.innerHTML= "<input type=file name=uploadFile["+ (tableFiles.rows.length-1) + "].file value=''/>"
}
function delFile(){
if (tableFiles.rows.length > 1){
tableFiles.deleteRow(tableFiles.rows.length-1);
}
}
function deleteOtherFiles(){
while(tableFiles.rows.length > 1){
tableFiles.deleteRow(tableFiles.rows.length-1);
}
}
function setAddDelBtnDisplay(){
var objChkFolder = document.getElementById("chkUploadFolder");
var objBtn = document.getElementById("RowAddDelBtn");
if(objChkFolder.checked){
objBtn.style.display="";
}else{
deleteOtherFiles();
objBtn.style.display="none";
}
}
function upload(){
multiUploadForm.submit();
}
</script>
<body bgcolor="#ffffff">
<html:form method="post" action="/multiUploadAction.do" enctype="multipart/form-data">
<table border="1">
<TBODY>
<tr>
<td>
上传文件(<input name="chkUploadFolder" type="checkbox" onclick="setAddDelBtnDisplay();"/>文件夹)
</td>
</tr>
<tr>
<td>
<table id="tableFiles">
<nested:iterate id="uploadFile" property="myFiles" name="multiUploadForm" indexId="index">
<nested:nest property="uploadFile">
<tr>
<td>
<nested:file property="file" name="uploadFile" indexed="true" />
</td>
</tr>
</nested:nest>
</nested:iterate>
</table>
</td>
</tr>
<TR id ="RowAddDelBtn" style="display: none;">
<td align="center">
<table>
<tr>
<td>
文件夹名:<input type="text" name="folderName" size="10"/>
</td>
<td>
<input type="button" name="btnAddFile" value="Add File" onclick="addFile()" />
</td>
<td>
<input type="button" name="btnAddFile" value="Del File" onclick="delFile()" />
</td>
</tr>
</table>
</td>
</TR>
<TR>
<td>
<input type="button" name="btnUpload" value="upload" onclick="upload()">
</td>
</TR>
</TBODY>
</table>
</html:form>
</body>
</html>
注意事项:
<html:form method="post" action="/multiUploadAction.do" enctype="multipart/form-data">中一定要加上 enctype="multipart/form-data"
否则,会出现类型不匹配的问题.
//ACTIONFORM
package upload.multi;
import java.util.ArrayList;
import java.util.List;
import org.apache.struts.action.ActionForm;
public class MultiUploadForm extends ActionForm {
private List myFiles;
// 上传的文件名
private String uploadFileName;
// 存入数据中的URL
private String fileUrl;
public MultiUploadForm() {
myFiles = new ArrayList();
//为了能够在页面初始显示一个file
myFiles.add(new UploadFile());
}
public List getMyFiles() {
return myFiles;
}
public String getFileUrl() {
return fileUrl;
}
public void setFileUrl(String fileUrl) {
this.fileUrl = fileUrl;
}
public String getUploadFileName() {
return uploadFileName;
}
public void setUploadFileName(String uploadFileName) {
this.uploadFileName = uploadFileName;
}
//注意这个方法的定义
public UploadFile getUploadFile(int index) {
int size = myFiles.size();
if (index > size - 1) {
for (int i = 0; i < index - size + 1; i++) {
myFiles.add(new UploadFile());
}
}
return (UploadFile) myFiles.get(index);
}
public void setMyFiles(List myFiles) {
this.myFiles = myFiles;
}
}
//UploadFile类
package upload.multi;
import java.io.Serializable;
import org.apache.struts.upload.FormFile;
public class UploadFile implements Serializable {
private FormFile file;
public FormFile getFile() {
System.out.println("run in uploadFile.getFile()");
return file;
}
public void setFile(FormFile file) {
this.file = file;
}
}
//action类
package upload.multi;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
public class MultiUploadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
MultiUploadForm multiUploadForm = (MultiUploadForm) form;
List myFiles = multiUploadForm.getMyFiles();
String filePath = servlet.getServletContext().getRealPath(
"/messageresponsefile/");//上传文件实际的存储的路径,文件内容存在硬盘上,而不是数据库中
try{
for(int i =0;i
UploadFile uploadFile = (UploadFile)myFiles.get(i);
FormFile file = uploadFile.getFile();
if(file==null){
System.out.println("file is null");
}else{
String fileName = formfile.getFileName().trim();
uploadFile(file,filePath, fileName);//读取文件
multiUploadForm.setUploadFileName(fileName);
multiUploadForm.setFileUrl("/messageresponsefile/"+ fileName);
//能运行到这里,就可以使用单个文件上传的方法进行上传了。循环而
//下面的代码是把ACTIONFORM中的数据传到MODEL或者说是"中间层"中的私有方法了,请自行定义
}
}catch(Exception e){
e.printStackTrace();
}
return null;
}
}
//uploadFile方法,读取上传的文件
public boolean uploadFile(FormFile file, String path, String name) {
String fileName = name;
File dir = new File(path);
if (!dir.exists())
dir.mkdir();
//文件存在了,删除
File f = new File(path + "/" + fileName);
if (f.exists())
f.delete();
try {
FileOutputStream out = new FileOutputStream(path + "/" + fileName);
byte[] binaryBytes = new byte[file.getFileSize()];
file.getInputStream().read(binaryBytes);
out.write(binaryBytes);
out.close();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
评论