Struts实现自动多文件上传
2007-05-31 17:20
483 查看
本文引自其它文章(具体地方和作者已经遗忘)并根据自己需求修改
Form部分:
public class MultiUploadForm extends ActionForm {
private List myFiles;
private String[] description = new String[100];
public MultiUploadForm(){
myFiles = new ArrayList();
//为了能够在页面初始显示一个file
myFiles.add(new UploadFile());
}
public List getMyFiles() {
return myFiles;
}
public String[] getDescription() {
return description;
}
public void setDescription(String[] description) {
this.description = description;
}
public void setDescription(int i ,String description) {
this.description[i] = description;
}
public String getDescription(int i) {
return description[i];
}
//注意这个方法的定义
public UploadFile getUploadFile(int index){
int size = myFiles.size();
if(index < size){
myFiles.set(index,new UploadFile());
}else{
for(int i = index ;i>size-1;i--){
myFiles.add(new UploadFile());
}
}
return (UploadFile)myFiles.get(index);
}
public void setMyFiles(List myFiles) {
this.myFiles = myFiles;
}
}
Dataset部分:
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部分:
public class MultiUploadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request,
HttpServletResponse response) {
MultiUploadForm multiUploadForm = (MultiUploadForm) form;
List myFiles = multiUploadForm.getMyFiles();
for(int i =0;i<myFiles.size();i++){
UploadFile uploadFile = (UploadFile)myFiles.get(i);
FormFile file = uploadFile.getFile();
if(file==null){
System.out.println("file is null");
}
else{
//能运行到这里,就可以使用单个文件上传的方法进行上传了。循环而已
System.out.println("filename:::" + file.getFileName());
System.out.println("file size:::" + file.getFileSize());
System.out.println("description:::" + file.getDescription());
}
}
return null;
}
}
JSP部分:
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<html:html>
<head>
<title>
multiUploadDemo
</title>
</head>
<script language="JavaScript">
var fileIndex= 0;
function addRow(){
//得到fileList这个id
var oFileList = document.getElementById("fileList");
//生成tr
var mytr=document.createElement("tr");
//得到整个fileList里的个数
//var fileIndex = oFileList.childNodes.length;
//设置tr的id
mytr.setAttribute("id","tr_" + fileIndex);
//生成一个td
var mytd2=document.createElement("td");
mytd2.setAttribute("width","300");
mytd2.setAttribute("align","center");
var myfile=document.createElement("input");
myfile.setAttribute("type","file");
myfile.setAttribute("size","18");
myfile.setAttribute("name","uploadFile["+ fileIndex + "].file");
myfile.setAttribute("id","file_" + fileIndex);
mytd2.appendChild(document.createTextNode("图片上传: "));
mytd2.appendChild(myfile);
mytr.appendChild(mytd2);
//生成一个td
var mytd1=document.createElement("td");
//设置td宽度
mytd1.setAttribute("width","450");
//td居中
mytd1.setAttribute("align","center");
//生成一个功能块(文本框,按钮)
var mytextfield1=document.createElement("input");
//设置属性
mytextfield1.setAttribute("type","text");
//设置名称
mytextfield1.setAttribute("name","description["+ fileIndex + "]");
//设置长度
mytextfield1.setAttribute("size","48");
//设置id
mytextfield1.setAttribute("id","title_" + fileIndex);
//加文字说明
mytd1.appendChild(document.createTextNode("图片说明: "));
//将功能块加入td中
mytd1.appendChild(mytextfield1);
//将td加入tr中
mytr.appendChild(mytd1);
var mytd3=document.createElement("td");
mytd3.setAttribute("width","50");
mytd3.setAttribute("align","center");
//**如果fuction不传参数就可用以下
//var mybutton=document.createElement("input");
//mybutton.setAttribute("type","button");
//mybutton.setAttribute("value","删除");
//mytd3.appendChild(mybutton);
//mybutton.onclick=removeFile(fileIndex);
//直接编写语句加入td中
mytd3.innerHTML = "<input type='button' name='button' value='删除' onclick='removeFile("+fileIndex+")'> ";
mytr.appendChild(mytd3);
oFileList.appendChild(mytr);
fileIndex++;
}
function removeFile(fileIndex){
//生成一个id里信息
var oTR = document.getElementById("tr_" + fileIndex);
//在tbody里删除id
fileList.removeChild(oTR);
}
</script>
<body>
<html:form method="POST" action="/multiUploadAction.do" enctype="multipart/form-data">
<table width="800" border="1" align="center">
<tr>
<td>
<input type="button" name="button" value="添加"
onclick="addRow()">
</td>
<td align="center" colspan="2"><html:submit value="提交" property="submit"></html:submit>
<html:reset value="重置" property="reset"></html:reset>
</td>
</tr>
</tbody>
</table>
<table width="800" border="0" align="center">
<TBODY id="fileList"></TBODY>
</table>
</html:form>
</body>
</html:html>
struts-config.xml部分:
<form-beans>
<form-bean name="multiUploadForm" type="MultiUploadForm" />
</form-beans>
<action name="multiUploadForm" path="/multiUploadAction" type="MultiUploadAction" />
Form部分:
public class MultiUploadForm extends ActionForm {
private List myFiles;
private String[] description = new String[100];
public MultiUploadForm(){
myFiles = new ArrayList();
//为了能够在页面初始显示一个file
myFiles.add(new UploadFile());
}
public List getMyFiles() {
return myFiles;
}
public String[] getDescription() {
return description;
}
public void setDescription(String[] description) {
this.description = description;
}
public void setDescription(int i ,String description) {
this.description[i] = description;
}
public String getDescription(int i) {
return description[i];
}
//注意这个方法的定义
public UploadFile getUploadFile(int index){
int size = myFiles.size();
if(index < size){
myFiles.set(index,new UploadFile());
}else{
for(int i = index ;i>size-1;i--){
myFiles.add(new UploadFile());
}
}
return (UploadFile)myFiles.get(index);
}
public void setMyFiles(List myFiles) {
this.myFiles = myFiles;
}
}
Dataset部分:
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部分:
public class MultiUploadAction extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request,
HttpServletResponse response) {
MultiUploadForm multiUploadForm = (MultiUploadForm) form;
List myFiles = multiUploadForm.getMyFiles();
for(int i =0;i<myFiles.size();i++){
UploadFile uploadFile = (UploadFile)myFiles.get(i);
FormFile file = uploadFile.getFile();
if(file==null){
System.out.println("file is null");
}
else{
//能运行到这里,就可以使用单个文件上传的方法进行上传了。循环而已
System.out.println("filename:::" + file.getFileName());
System.out.println("file size:::" + file.getFileSize());
System.out.println("description:::" + file.getDescription());
}
}
return null;
}
}
JSP部分:
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-nested.tld" prefix="nested" %>
<html:html>
<head>
<title>
multiUploadDemo
</title>
</head>
<script language="JavaScript">
var fileIndex= 0;
function addRow(){
//得到fileList这个id
var oFileList = document.getElementById("fileList");
//生成tr
var mytr=document.createElement("tr");
//得到整个fileList里的个数
//var fileIndex = oFileList.childNodes.length;
//设置tr的id
mytr.setAttribute("id","tr_" + fileIndex);
//生成一个td
var mytd2=document.createElement("td");
mytd2.setAttribute("width","300");
mytd2.setAttribute("align","center");
var myfile=document.createElement("input");
myfile.setAttribute("type","file");
myfile.setAttribute("size","18");
myfile.setAttribute("name","uploadFile["+ fileIndex + "].file");
myfile.setAttribute("id","file_" + fileIndex);
mytd2.appendChild(document.createTextNode("图片上传: "));
mytd2.appendChild(myfile);
mytr.appendChild(mytd2);
//生成一个td
var mytd1=document.createElement("td");
//设置td宽度
mytd1.setAttribute("width","450");
//td居中
mytd1.setAttribute("align","center");
//生成一个功能块(文本框,按钮)
var mytextfield1=document.createElement("input");
//设置属性
mytextfield1.setAttribute("type","text");
//设置名称
mytextfield1.setAttribute("name","description["+ fileIndex + "]");
//设置长度
mytextfield1.setAttribute("size","48");
//设置id
mytextfield1.setAttribute("id","title_" + fileIndex);
//加文字说明
mytd1.appendChild(document.createTextNode("图片说明: "));
//将功能块加入td中
mytd1.appendChild(mytextfield1);
//将td加入tr中
mytr.appendChild(mytd1);
var mytd3=document.createElement("td");
mytd3.setAttribute("width","50");
mytd3.setAttribute("align","center");
//**如果fuction不传参数就可用以下
//var mybutton=document.createElement("input");
//mybutton.setAttribute("type","button");
//mybutton.setAttribute("value","删除");
//mytd3.appendChild(mybutton);
//mybutton.onclick=removeFile(fileIndex);
//直接编写语句加入td中
mytd3.innerHTML = "<input type='button' name='button' value='删除' onclick='removeFile("+fileIndex+")'> ";
mytr.appendChild(mytd3);
oFileList.appendChild(mytr);
fileIndex++;
}
function removeFile(fileIndex){
//生成一个id里信息
var oTR = document.getElementById("tr_" + fileIndex);
//在tbody里删除id
fileList.removeChild(oTR);
}
</script>
<body>
<html:form method="POST" action="/multiUploadAction.do" enctype="multipart/form-data">
<table width="800" border="1" align="center">
<tr>
<td>
<input type="button" name="button" value="添加"
onclick="addRow()">
</td>
<td align="center" colspan="2"><html:submit value="提交" property="submit"></html:submit>
<html:reset value="重置" property="reset"></html:reset>
</td>
</tr>
</tbody>
</table>
<table width="800" border="0" align="center">
<TBODY id="fileList"></TBODY>
</table>
</html:form>
</body>
</html:html>
struts-config.xml部分:
<form-beans>
<form-bean name="multiUploadForm" type="MultiUploadForm" />
</form-beans>
<action name="multiUploadForm" path="/multiUploadAction" type="MultiUploadAction" />
相关文章推荐
- 在Struts 2中实现文件上传(Struts 2.0系列之九)
- 用winrar和ftp命令实现自动备份文件并自动上传到指定的ftp服务器
- struts2+AJAX文件上传进度条的基本实现
- 在Struts 2中实现文件上传
- struts--实现上传文件
- 在Struts 2中实现文件上传
- 在Struts 2中实现文件上传
- bootstrapfileinput实现文件自动上传
- 在Struts 2中实现文件上传
- Struts实现简单的文件下载(继上一篇文件上传)
- CentOS中使用Shell脚本实现每天自动备份网站文件和数据库并上传到FTP中
- Struts2SH整合uploadify实现文件上传《按钮中文解决》
- struts实现多文件上传
- 在Struts 2中实现文件上传
- 如果用struts实现动态(不定数量)多文件上传?
- java通过struts实现web中的文件上传
- python 实现自动上传文件到百度网盘(附程序源码及实现过程)
- struts1 实现 文件上传
- 【Android】使用Jenkins+Gradle+FTP,实现自动打包,自动上传文件至FTP
- 用struts实现单个文件上传并改变上传图片的大小