您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 市场营销 > Spring MVC uploadify2.1批量文件、图片上传
SpringMVCuploadify2.1批量文件、图片上传今天在做一个批量上传图片的需求,google下,觉得uploadify在jquery的上传控件还是挺不错,特写下怎么使用。1、下载资源包,2.1.0整理,免费分享地址、需要的资源(1):jquery-1.3.2.min.js(2):jquery.uploadify.v2.1.0.min.js(3):swfobject.js(4):uploadify.css(5):uploadify.swf3、页面引用4、使用[javascript]viewplaincopyprint?$(document).ready(function(){$(#multiple_file_upload).uploadify({'uploader':'${base}/thirdparty/uploadify/uploadify.swf?random='+(newDate()).getTime(),'cancelImg':'${base}/thirdparty/uploadify/cancel.png','script':'../common/o_multiple_upload.do',//要提交到的处理文件上传的PHP文件'auto':false,//是否自动开始'multi':true,//是否支持多文件上传'buttonText':'browe',//按钮上的文字'simUploadLimit':1000,//一次同步上传的文件数目'sizeLimit':19871202,//设置单个文件大小限制'queueSizeLimit':1000,//队列中同时存在的文件个数限制'fileDesc':'支持格式:jpg/gif/jpeg/png/bmp.',//如果配置了以下的'fileExt'属性,那么这个属性是必须的'fileExt':'*.jpg;*.gif;*.jpeg;*.png;*.bmp',//允许的格式onComplete:function(event,queueID,fileObj,response,data){//$('').appendTo('.files').text(response);varpicIndexPlus=picIndex++;varuploadPath=response;$('#picBefore').before(picTpl(picIndexPlus));varuploadImgPathId=uploadImgPath+(picIndexPlus);document.getElementById(uploadImgPathId).value=uploadPath;},onError:function(event,queueID,fileObj){alert(文件:+fileObj.name+上传失败);},onCancel:function(event,queueID,fileObj){//alert(取消了+fileObj.name);}});});$(document).ready(function(){$(#multiple_file_upload).uploadify({'uploader':'${base}/thirdparty/uploadify/uploadify.swf?random='+(newDate()).getTime(),'cancelImg':'${base}/thirdparty/uploadify/cancel.png','script':'../common/o_multiple_upload.do',//要提交到的处理文件上传的PHP文件'auto':false,//是否自动开始'multi':true,//是否支持多文件上传'buttonText':'browe',//按钮上的文字'simUploadLimit':1000,//一次同步上传的文件数目'sizeLimit':19871202,//设置单个文件大小限制'queueSizeLimit':1000,//队列中同时存在的文件个数限制'fileDesc':'支持格式:jpg/gif/jpeg/png/bmp.',//如果配置了以下的'fileExt'属性,那么这个属性是必须的'fileExt':'*.jpg;*.gif;*.jpeg;*.png;*.bmp',//允许的格式onComplete:function(event,queueID,fileObj,response,data){//$('').appendTo('.files').text(response);varpicIndexPlus=picIndex++;varuploadPath=response;$('#picBefore').before(picTpl(picIndexPlus));varuploadImgPathId=uploadImgPath+(picIndexPlus);document.getElementById(uploadImgPathId).value=uploadPath;},onError:function(event,queueID,fileObj){alert(文件:+fileObj.name+上传失败);},onCancel:function(event,queueID,fileObj){//alert(取消了+fileObj.name);}});});5、后台代码Java代码[java]viewplaincopyprint?/***批量上传图片**@paramfilename文件名*@paramuploadNum上传数量*@parammark*@paramfile文件流*@paramrequest*@parammodel*@return*@throwsException*/@RequestMapping(value=/common/o_multiple_upload.do)public@ResponseBodyStringexecuteMultiple(Stringfilename,IntegeruploadNum,Booleanmark,HttpServletRequestrequest,HttpServletResponseresponse,ModelMapmodel)throwsException{MultipartHttpServletRequestmultipartRequest=(MultipartHttpServletRequest)request;MapfileMap=multipartRequest.getFileMap();//存储fileUrlListuploadPaths=newArrayList();StringfileUrl=null;for(Map.Entryentity:fileMap.entrySet()){//获取MulipartFile对象MultipartFilefile=entity.getValue();WebErrorserrors=validate(filename,file,request);if(errors.hasErrors()){model.addAttribute(ERROR,errors.getErrors().get(0));returnRESULT_PAGE;}CmsSitesite=CmsUtils.getSite(request);MarkConfigconf=site.getConfig().getMarkConfig();if(mark==null){mark=conf.getOn();}//上传文件名StringorigName=file.getOriginalFilename();Stringext=FilenameUtils.getExtension(origName).toLowerCase(Locale.ENGLISH);try{if(site.getConfig().getUploadToDb()){StringdbFilePath=site.getConfig().getDbFileUri();if(!StringUtils.isBlank(filename)){filename=filename.substring(dbFilePath.length());if(mark){FiletempFile=mark(file,conf);fileUrl=dbFileMng.storeByFilename(filename,newFileInputStream(tempFile));tempFile.delete();}else{fileUrl=dbFileMng.storeByFilename(filename,file.getInputStream());}}else{if(mark){FiletempFile=mark(file,conf);fileUrl=dbFileMng.storeByExt(site.getUploadPath(),ext,newFileInputStream(tempFile));tempFile.delete();}else{fileUrl=dbFileMng.storeByExt(site.getUploadPath(),ext,file.getInputStream());}//加上访问地址fileUrl=request.getContextPath()+dbFilePath+fileUrl;}}elseif(site.getUploadFtp()!=null){Ftpftp=site.getUploadFtp();StringftpUrl=ftp.getUrl();if(!StringUtils.isBlank(filename)){filename=filename.substring(ftpUrl.length());if(mark){FiletempFile=mark(file,conf);fileUrl=ftp.storeByFilename(filename,newFileInputStream(tempFile));tempFile.delete();}else{fileUrl=ftp.storeByFilename(filename,file.getInputStream());}}else{if(mark){FiletempFile=mark(file,conf);fileUrl=ftp.storeByExt(site.getUploadPath(),ext,newFileInputStream(tempFile));tempFile.delete();}else{fileUrl=ftp.storeByExt(site.getUploadPath(),ext,file.getInputStream());}//加上url前缀fileUrl=ftpUrl+fileUrl;}}else{Stringctx=request.getContextPath();if(!StringUtils.isBlank(filename)){filename=filename.substring(ctx.length());if(mark){FiletempFile=mark(file,conf);fileUrl=fileRepository.storeByFilename(filename,tempFile);tempFile.delete();}else{fileUrl=fileRepository.storeByFilename(filename,file);}}else{if(mark){FiletempFile=mark(
本文标题:Spring MVC uploadify2.1批量文件、图片上传
链接地址:https://www.777doc.com/doc-3308106 .html