多文件上传

一、到http://www.uploadify.com/下载最新版,我的是jquery.uploadify-v1.6.2 (MIT)
二、下载包解压放在对应位置
三、HttpServlet,如下:
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        ServletContext servletContext = this.getServletConfig().getServletContext();
        ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UploadFileDao dao = (UploadFileDao) appContext.getBean("uploadFileDao");
        String savePath = this.getServletConfig().getServletContext()
        .getRealPath("/")
        + UPLOAD_FOLDER;
        DiskFileItemFactory fac = new DiskFileItemFactory();
        ServletFileUpload upload = new ServletFileUpload(fac);
        List fileList = null;
        try {
            fileList = upload.parseRequest(req);
        } catch (FileUploadException ex) {
            ex.printStackTrace();
            return;
        }
        Iterator<FileItem> it = fileList.iterator();
        StringBuilder sb = new StringBuilder();
        while(it.hasNext()){
            FileItem item =  it.next();
            if(!item.isFormField()){
                String name = item.getName();
                long size = item.getSize();
                String type = item.getContentType();
                if(StringUtils.isBlank(name))
                    continue;
                File saveFile = new File(savePath+name);
                try {
                    item.write(saveFile);
                    String format = "yyyy/MM/dd hh:mm:ss";
                    UploadFile uploadFile = new UploadFile();
                    uploadFile.setCreateTime(DateUtil.getCurDateString(format));
                    uploadFile.setName(UUID.randomUUID().toString());
                    uploadFile.setOrgName(name);
                    uploadFile.setType(type);
                    uploadFile.setSize(size);
                    dao.save(uploadFile);
                    if(StringUtils.isBlank(sb.toString()))
                        sb.append(uploadFile.getId());
                    else
                        sb.append(";"+uploadFile.getId());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
       
        resp.getWriter().print(sb.toString());
    }

四、对应调用页面,jsp:
引入必要的css和javascript
头部:

//upload file
            $("#fileUpload").fileUpload({
                'uploader': '${ctx}/js/uploadify/uploader.swf',
                'cancelImg': '${ctx}/js/uploadify/cancel.png',
                'script': '${ctx}/file/upload',
                'folder': 'upload',
                'multi': true,
                'buttonText': 'Select Files',
                'displayData': 'speed',
                onComplete:    function(event,queueID,fileObj,response,data) {
                                var fm = document.forms[0];
                                var fileInputs = document.getElementsByName("logos");
                                for(var i=0;i<fileInputs.length;i++){
                                    fm.removeChild(fileInputs[i]);
                                }
                                //生成隐藏字段
                                var fileInput = document.createElement("input");
                                fileInput.setAttribute("name","logos");
                                fileInput.setAttribute("value",fileObj.name);
                                fileInput.setAttribute("type","hidden");
                                fm.appendChild(fileInput);
                }
            });


页面body:
<div id="fileUpload">You have a problem with your javascript</div>
<a href="javascript:$('#fileUpload').fileUploadStart()">Start Upload</a> |  <a href="javascript:$('#fileUpload').fileUploadClearQueue()">Clear Queue</a>


你可能感兴趣的:(JavaScript,jquery,IE,Flash,Adobe)