`
giky2323
  • 浏览: 28149 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
最近访客 更多访客>>
文章分类
社区版块
存档分类
最新评论

struts动态添加多附件上传功能(和163一样一个个的加的那种)

阅读更多

要注意的几点:

1.jsp页面。

1.1提交的form要写enctype="multipart/form-data",

<html:form action="/gamearticlesae?method=addnew" enctype="multipart/form-data"> </html:form>

1.2jsp页面用JS动态添加控件的方法:

js 代码
  1. <script>   
  2. var idIndex=0;   
  3. function addFile()   
  4.   {//添加表格的一行   
  5.   oTR=idTB.insertRow(idTB.rows.length);   
  6.   tmpNum=oTR.rowIndex;   
  7.   oTD=oTR.insertCell(0);   
  8.   oTD.innerText = tmpNum;//在该处添加的HTML代码会原封不动的显示在页面上   
  9.   alert(tmpNum);   
  10.   oTD.innerHTML ="<input type='file' name='file("< span="">+tmpNum+")'>  <button onclick='removefile();'>delete
    "
    ;//要在该格添加的HTML代码填在这里,因为这里是text,注意不要重名了。   
  11.   idLast.innerText=idTB.rows.length;   
  12.   if(idTB.rows.length>0)   
  13.     idFirst.innerText='1';   
  14.   return true;   
  15. }   
  16. function removeFile()   
  17.   {//删除表格的一行   
  18.   sIndex=idIndex.value;   
  19.   if(sIndex=='')   
  20.     sIndex=idTB.rows.length-1;   
  21.   else  
  22.     sIndex=parseInt(sIndex)-1;   
  23.        
  24.  idTB.deleteRow(sIndex);   
  25.      
  26.   idLast.innerText=idTB.rows.length;   
  27.   if(idTB.rows.length==0)   
  28.     idFirst.innerText='0';   
  29. }   
  30. </script>   

相应的:

js 代码
  1.     "addFile()" value="添加附件">   
  2.     
  3.    

2.actionform的问题

因为附件的个数是不确定的,所以用

java 代码
  1. private List files = new ArrayList();   
  2.   
  3.  public FormFile getFile(String index) {   
  4.      return (FormFile) files.get(Integer.parseInt(index));   
  5.     }   
  6.   
  7.     public void setFile(String index, FormFile file) {   
  8.      this.files.add(file);   
  9.     }   
  10.   
  11.     public List getFiles() {   
  12.      return this.files;   
  13.     }   
  14.   

3.action

       java 代码

  1. Hashtable fileh=gamearticlesaeForm.getMultipartRequestHandler().getFileElements();   
  2. for (Enumeration e = fileh.keys(); e.hasMoreElements(); ) {    
  3.     String key = (String) e.nextElement();    
  4.     try {    
  5.     FormFile formfile = (FormFile) fileh.get(key);    
  6.     String filename = formfile.getFileName().trim(); //文件名    
  7.     log.info("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ the filename is "+filename);   
  8.     }   
  9.     catch(Exception sse){}   
  10. }   

 

翻了下孙MM的书,

java 代码
<html:file property="xxx">
  1. "file" NAME="xxx">

 

</html:file>
这样就OK了
分享到:
评论
5 楼 elvishehai 2009-07-28  
这个好像不是很快啊,  写一个线程来做吧
4 楼 rickhunter 2009-04-24  
请LZ提供完整点的代码,为什么我只能得到一个附件。

<html:file property="xxx"> 这个XXX在formbean中怎么定义。
3 楼 hanyuhaha 2008-07-31  
嗯,好贴!
2 楼 giky2323 2007-12-23  
再仔细看看你的东东,我写了几次了都是可以的。
1 楼 zlsunnan 2007-11-24  
为什么  Hashtable fileh=gamearticlesaeForm.getMultipartRequestHandler().getFileElements();      取不到呢

相关推荐

Global site tag (gtag.js) - Google Analytics