注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

骇客归来

ぁ枫あ

 
 
 

日志

 
 

struts中不限数量的多文件上传  

2006-12-27 14:02:32|  分类: Java |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |

Server Code:

String dirPath = getServlet().getServletContext().getRealPath("/")
                         + "/upload";
        Hashtable fileh = actionForm.getMultipartRequestHandler().
                          getFileElements();
        for (Enumeration e = fileh.keys(); e.hasMoreElements(); ) {
            String key = (String) e.nextElement();
            try {
                FormFile formfile = (FormFile) fileh.get(key);
                String filename = formfile.getFileName().trim(); //文件名
                if (!"".equals(filename)) {
                    //不同的浏览器传上的文件名可能有区别,有的是全路径的
                    //在这里保存文件
                    InputStream ins = formfile.getInputStream();
                    OutputStream os = new FileOutputStream(dirPath +
                            File.separatorChar
                            + filename);
                    int bytesRead = 0;
                    byte[] buffer = new byte[8192];
                    while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                        os.write(buffer, 0, bytesRead);
                    }
                    os.close();
                    ins.close();

                }
            } catch (Exception ex) {
                logger.debug("出错了", ex);
            }
        }

 

 

Client

 <script language="JavaScript">
        function addfile() {
            var count = document.getElementById("count").value;
            document.getElementById("count").value = Number(count) + 1;
           
            var fileContainer = document.getElementById("filelist");
           
            var tmpFile  = document.createElement("input");
            tmpFile.type = "file";
            tmpFile.id = "file_" + count;
            //tmpFile.name = "file";
            tmpFile.name = "file_" + count;
            fileContainer.appendChild(tmpFile);
           
            var delButton = document.createElement("input");
            delButton.type = "button";
            delButton.id = "delButton_" + count;
            //delButton.name = "delButton";
            delButton.name = "delButton_" + count;
            delButton.value = "Delete";
            delButton.onclick = function() {
                                var tmpFile = document.getElementById("file_" + count);
                                var tmpButton = document.getElementById("delButton_" + count);
                                var tmpBr = document.getElementById("br_" + count);
                                var fileContainer = document.getElementById("filelist");
                               
                                fileContainer.removeChild(tmpFile);
                                fileContainer.removeChild(tmpButton);
                                fileContainer.removeChild(tmpBr);
                            }
            fileContainer.appendChild(delButton);
           
            var br = document.createElement("<br/>");
            br.id = "br_" + count;
            fileContainer.appendChild(br);
        }
       
        function removeDefaultFile() {
            var index = 0;
            var tmpFile = document.getElementById("file_" + index);
            var tmpButton = document.getElementById("delButton_" + index);
            var tmpBr = document.getElementById("br_" + index);
            var fileContainer = document.getElementById("filelist");
           
            fileContainer.removeChild(tmpFile);
            fileContainer.removeChild(tmpButton);
            fileContainer.removeChild(tmpBr);
        }
       
        function removeOldFile(num) {
            var index = num;
            var tmpFile = document.getElementById("oldFile_" + index);
            var tmpButton = document.getElementById("oldFileDelButton_" + index);
            var tmpBr = document.getElementById("br_oldFile_" + index);
            var fileContainer = document.getElementById("filelist");
           
            fileContainer.removeChild(tmpFile);
            fileContainer.removeChild(tmpButton);
            fileContainer.removeChild(tmpBr);
        }

</script>

 

Html Page Code:

<p >
                        <div >
                            <input type="hidden" value="1" ><br>
                            <input type="file" ><input type="button" value="Delete" ><br />
                        </div>
     </p>
     <p><span ><a href=";">Add</a></span></p>

  评论这张
 
阅读(73)| 评论(0)
推荐 转载

历史上的今天

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017