SpringBoot+layui实现文件上传功能
什么是springboot
SpringBoot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。用我的话来理解,就是springboot其实不是什么新的框架,它默认配置了很多框架的使用方式,就像maven整合了所有的jar包,springboot整合了所有的框架(不知道这样比喻是否合适)。
页面代码(只需要引入基础layui的css与js)
选择多文件 文件名 大小 状态 操作
JS
layui.use('upload',function(){
var$=layui.jquery
,upload=layui.upload;
//多文件列表示例
vardemoListView=$('#demoList')
,uploadListIns=upload.render({
elem:'#testList'
,url:'upload/uploadFile'
,accept:'file'
,multiple:true
,auto:false
,size:5120
,bindAction:'#testListAction'
,choose:function(obj){
varfiles=this.files=obj.pushFile();//将每次选择的文件追加到文件队列
//读取本地文件
obj.preview(function(index,file,result){
vartr=$([''
,''+file.name+' '
,''+(file.size/1014).toFixed(1)+'kb '
,'等待上传 '
,''
,'重传'
,'删除'
,' '
,''].join(''));
//单个重传
tr.find('.demo-reload').on('click',function(){
obj.upload(index,file);
});
//删除
tr.find('.demo-delete').on('click',function(){
deletefiles[index];//删除对应的文件
tr.remove();
uploadListIns.config.elem.next()[0].value='';//清空inputfile值,以免删除后出现同名文件不可选
});
demoListView.append(tr);
});
}
,done:function(res,index,upload){
if(res.code==0){//上传成功
vartr=demoListView.find('tr#upload-'+index)
,tds=tr.children();
tds.eq(2).html('上传成功');
tds.eq(3).html('');//清空操作
returndeletethis.files[index];//删除文件队列已经上传成功的文件
}
this.error(index,upload);
}
,error:function(index,upload){
vartr=demoListView.find('tr#upload-'+index)
,tds=tr.children();
tds.eq(2).html('上传失败');
tds.eq(3).find('.demo-reload').removeClass('layui-hide');//显示重传
}
});
});
后台接收
publicfinalstaticStringUPLOAD_FILE_PATH="D:\\uploadFile\\";
@RequestMapping(value="uploadFile")
publicStringuploadImage(@RequestParam("file")MultipartFilefile){
if(!file.isEmpty()){
MapresObj=newHashMap<>(MAP_SIZE);
try{
BufferedOutputStreamout=newBufferedOutputStream(
newFileOutputStream(newFile(UPLOAD_FILE_PATH,file.getOriginalFilename())));
out.write(file.getBytes());
out.flush();
out.close();
}catch(IOExceptione){
resObj.put("msg","error");
resObj.put("code","1");
returnJSONObject.toJSONString(resObj);
}
resObj.put("msg","ok");
resObj.put("code","0");
returnJSONObject.toJSONString(resObj);
}else{
returnnull;
}
}
总结
以上所述是小编给大家介绍的SpringBoot+layui实现文件上传功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。