Spring MVC uploadify21批量文件图片上传.docx
- 文档编号:7378249
- 上传时间:2023-05-11
- 格式:DOCX
- 页数:14
- 大小:16.66KB
Spring MVC uploadify21批量文件图片上传.docx
《Spring MVC uploadify21批量文件图片上传.docx》由会员分享,可在线阅读,更多相关《Spring MVC uploadify21批量文件图片上传.docx(14页珍藏版)》请在冰点文库上搜索。
SpringMVCuploadify21批量文件图片上传
SpringMVCuploadify2.1批量文件、图片上传
今天在做一个批量上传图片的需求,google下,觉得uploadify在jquery的上传控件还是挺不错,特写下怎么使用。
1、下载资源包,2.1.0整理,免费分享地址
2、需要的资源
(1):
jquery-1.3.2.min.js
(2):
jquery.uploadify.v2.1.0.min.js
(3):
swfobject.js
(4):
uploadify.css
(5):
uploadify.swf
3、页面引用
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@ResponseBody
StringexecuteMultiple(Stringfilename,IntegeruploadNum,Booleanmark,HttpServletRequestrequest,
HttpServletResponseresponse,ModelMapmodel)throwsException
{
MultipartHttpServletRequestmultipartRequest=(MultipartHttpServletRequest)request;
MapfileMap=multipartRequest.getFileMap();
//存储fileUrl
ListuploadPaths=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(file,conf);
fileUrl=fileRepository.storeByExt(site.getUploadPath(),ext,tempFile);
tempFile.delete();
}
else
{
fileUrl=fileRepository.storeByExt(site.getUploadPath(),ext,file);
}
//加上部署路径
fileUrl=ctx+fileUrl;
}
}
fileMng.saveFileByPath(fileUrl,origName,false);
uploadPaths.add(fileUrl);
model.addAttribute("uploadNum",uploadNum);
}
catch(IllegalStateExceptione)
{
model.addAttribute(ERROR,e.getMessage());
log.error("uploadfileerror!
",e);
}
catch(IOExceptione)
{
model.addAttribute(ERROR,e.getMessage());
log.error("uploadfileerror!
",e);
}
catch(Exceptione)
{
model.addAttribute(ERROR,e.getMessage());
log.error("uploadfileerror!
",e);
}
}
//model.addAttribute("uploadPaths",uploadPaths);
returnfileUrl;
}
/**
*批量上传图片
*
*@paramfilename文件名
*@paramuploadNum上传数量
*@parammark
*@paramfile文件流
*@paramrequest
*@parammodel
*@return
*@throwsException
*/
@RequestMapping(value="/common/o_multiple_upload.do")
public@ResponseBody
StringexecuteMultiple(Stringfilename,IntegeruploadNum,Booleanmark,HttpServletRequestrequest,
HttpServletResponseresponse,ModelMapmodel)throwsException
{
MultipartHttpServletRequestmultipartRequest=(MultipartHttpServletRequest)request;
MapfileMap=multipartRequest.getFileMap();
//存储fileUrl
ListuploadPaths=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(!
StringUti
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Spring MVC uploadify21批量文件图片上传 uploadify21 批量 文件 图片 上传