1.jsp页面
思路:根据你所选择的下拉框中的个数,显示多个文件上传列表。
<%@ page contentType="text/html;charset=gbk"%>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic"%>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested"%>
<%@ taglib uri="/WEB-INF/struts-tiles.tld" prefix="tiles"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<html>
<head>
<title>客户文档管理</title>
<meta http-equiv="Content-Type" content="text/html;charset=gbk">
</head>
<body>
<br />
<form action="" method="post" enctype="multipart/form-data" name="thisForm">
<input type="hidden" name="ID">
<table border='0' cellpadding='4' align="center" cellspacing='1'
id="table1" class="table" width="550">
<tr>
<td nowrap class='tdrighttextred'>
文档上传:
</td>
<td class='td' colspan="3">
<select name="fileNum" size="1" οnchange="addFile(this.value)">
<option value="0">
请选择文档个数
</option>
<c:forEach begin="1" end="50" var="i" step="1">
<option value="${i}">
${i}
</option>
</c:forEach>
</select>
<c:forEach begin="1" end="50" var="i" step="1">
<div style="width:100px"><input type="file" name="referenceFormFile[${i-1}]"/></div>
</c:forEach>
<input type='button' class="tbbutton" value='重选' id="filebutton"
οnclick='ResetFile()'>
</td>
</tr>
<tr class="trcenter">
<td height="25" colspan='4' nowrap>
<input name="savebutton" id="savebutton" type="button" class="tbbutton" value="保 存" οnclick="SubmitButton(this,'add');">
<input name="closebutton" id="closebutton" type="button" class="tbbutton" value="关 闭" οnclick="WinClose();">
</td>
</tr>
</table>
</form>
</body>
</html>
<script language="javascript" type="text/javascript">
initPage();
function initPage()
{
<!--初始的时候,首先将“重置,和文件选择列表隐藏,文件列表默认为50个”-->
document.getElementById('filebutton').style.display='none';
for(var i=0;i<=49;i++)
{
document.getElementById("referenceFormFile["+i+"]").style.display="none";
}
}
function addFile(n)
{
<!--当选择了文件个数后,隐藏文件选择框,出现上传文件列表-->
document.thisForm.fileNum.style.display='none';
document.getElementById("filebutton").style.display='';
if(n>0&&n<=50){
for(var i=0;i<n;i++){
document.getElementsByName("referenceFormFile["+i+"]")[0].style.display='';
//document.getElementById("referenceFormFileSpan["+i+"]").style.display='';
}
}else{
document.thisForm.fileNum.style.display='';
}
}
function ResetFile()
{
document.thisForm.fileNum.style.display='';
document.getElementById("filebutton").style.display='none';
document.thisForm.fileNum.options[0].selected=true;
for(var i=0;i<=49;i++){
document.getElementsByName("referenceFormFile["+i+"]")[0].style.display='none';
//document.getElementById("referenceFormFileSpan["+i+"]").style.display='none';
}
}
function SubmitButton(obj,SubmitFlag)
{
if(SubmitFlag=="add"){
document.thisForm.action="/BossPractice/CusFileAction.do";
document.thisForm.submit();
}
}
</script>
2.上传的JAVA文件
package com.bossfile;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
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 CusFileAction extends Action {
public ActionForward execute(ActionMapping map, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// 定义局部变量
String ID = "", submitButtonFlag = "", procMsg = "", fileExt = "";
int PhotosCount = 0;
CusFileForm actionform = (CusFileForm) form;
try {
FormFile formFile[] = actionform.getReferenceFormFile();
String path = "d://upload";
File file = new File(path);
file.mkdirs();
for (int i = 0; i < Integer.parseInt(actionform.getFileNum()); i++) {
if (formFile[i].getFileSize() != 0) { // 判断是否为空
fileExt = formFile[i].getFileName().substring(
formFile[i].getFileName().lastIndexOf("."),
formFile[i].getFileName().length()).toLowerCase();
PhotosCount += 1;
String filename = actionform.getId() + "-" + PhotosCount
+ fileExt;
uploadFile(path, formFile[i], filename);
}
}
} catch (Exception e) {
procMsg = e.getMessage();
System.out.print(e.getMessage());
e.printStackTrace();
} finally {
}
return null;
}
/**
* 文件上传功能
*/
public void uploadFile(String path, FormFile myFile, String fileName) {
try {
byte[] fileData = myFile.getFileData();
FileOutputStream fos = new FileOutputStream(new File(path
+ fileName));
fos.write(fileData);
fos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}