javascript
springboot 上传文件解析入库_SpringBoot + easyexcel + WebUploader 实现文件上传并解析
1. WebUploader的使用,引入css和js,css其實沒什么用:
2.?定義上傳框:
選擇文件開始上傳
3.相關jquery:
//?文件上傳
jQuery(function()?{
var?$?=?jQuery,
$list?=?$('#thelist'),
$btn?=?$('#ctlBtn'),
state?=?'pending',
uploader;
uploader?=?WebUploader.create({
//?不壓縮image
resize:?false,
//?swf文件路徑
swf:?'/js/Uploader.swf',
//?文件接收服務端。
server:?'http://webuploader.duapp.com/server/fileupload.php',
//?選擇文件的按鈕。可選。
//?內(nèi)部根據(jù)當前運行是創(chuàng)建,可能是input元素,也可能是flash.
pick:?'#picker'
});
//?當有文件添加進來的時候
uploader.on(?'fileQueued',?function(?file?)?{
$list.append(?'
'?+'
'?+?file.name?+?'
'?+'
等待上傳...
'?+'
'?);});
//?文件上傳過程中創(chuàng)建進度條實時顯示。
uploader.on(?'uploadProgress',?function(?file,?percentage?)?{
var?$li?=?$(?'#'+file.id?),
$percent?=?$li.find('.progress?.progress-bar');
//?避免重復創(chuàng)建
if?(?!$percent.length?)?{
$percent?=?$('
'?+'
'?+'
'?+'
').appendTo(?$li?).find('.progress-bar');}
$li.find('p.state').text('上傳中');
$percent.css(?'width',?percentage?*?100?+?'%'?);
});
uploader.on(?'uploadSuccess',?function(?file?)?{
$(?'#'+file.id?).find('p.state').text('已上傳');
});
uploader.on(?'uploadError',?function(?file?)?{
$(?'#'+file.id?).find('p.state').text('上傳出錯');
});
uploader.on(?'uploadComplete',?function(?file?)?{
$(?'#'+file.id?).find('.progress').fadeOut();
});
uploader.on(?'all',?function(?type?)?{
if?(?type?===?'startUpload'?)?{
state?=?'uploading';
}?else?if?(?type?===?'stopUpload'?)?{
state?=?'paused';
}?else?if?(?type?===?'uploadFinished'?)?{
state?=?'done';
}
if?(?state?===?'uploading'?)?{
$btn.text('暫停上傳');
}?else?{
$btn.text('開始上傳');
}
});
$btn.on(?'click',?function()?{
if?(?state?===?'uploading'?)?{
uploader.stop();
}?else?{
uploader.upload();
}
});
});
4.?這樣就可以了,貼一下完整代碼:html>
WebUploader選擇文件開始上傳
//?文件上傳
jQuery(function()?{
var?$?=?jQuery,
$list?=?$('#thelist'),
$btn?=?$('#ctlBtn'),
state?=?'pending',
uploader;
uploader?=?WebUploader.create({
//?不壓縮image
resize:?false,
//?swf文件路徑
swf:?'/js/Uploader.swf',
//?文件接收服務端。
server:?'http://webuploader.duapp.com/server/fileupload.php',
//?選擇文件的按鈕。可選。
//?內(nèi)部根據(jù)當前運行是創(chuàng)建,可能是input元素,也可能是flash.
pick:?'#picker'
});
//?當有文件添加進來的時候
uploader.on(?'fileQueued',?function(?file?)?{
$list.append(?'
'?+'
'?+?file.name?+?'
'?+'
等待上傳...
'?+'
'?);});
//?文件上傳過程中創(chuàng)建進度條實時顯示。
uploader.on(?'uploadProgress',?function(?file,?percentage?)?{
var?$li?=?$(?'#'+file.id?),
$percent?=?$li.find('.progress?.progress-bar');
//?避免重復創(chuàng)建
if?(?!$percent.length?)?{
$percent?=?$('
'?+'
'?+'
'?+'
').appendTo(?$li?).find('.progress-bar');}
$li.find('p.state').text('上傳中');
$percent.css(?'width',?percentage?*?100?+?'%'?);
});
uploader.on(?'uploadSuccess',?function(?file?)?{
$(?'#'+file.id?).find('p.state').text('已上傳');
});
uploader.on(?'uploadError',?function(?file?)?{
$(?'#'+file.id?).find('p.state').text('上傳出錯');
});
uploader.on(?'uploadComplete',?function(?file?)?{
$(?'#'+file.id?).find('.progress').fadeOut();
});
uploader.on(?'all',?function(?type?)?{
if?(?type?===?'startUpload'?)?{
state?=?'uploading';
}?else?if?(?type?===?'stopUpload'?)?{
state?=?'paused';
}?else?if?(?type?===?'uploadFinished'?)?{
state?=?'done';
}
if?(?state?===?'uploading'?)?{
$btn.text('暫停上傳');
}?else?{
$btn.text('開始上傳');
}
});
$btn.on(?'click',?function()?{
if?(?state?===?'uploading'?)?{
uploader.stop();
}?else?{
uploader.upload();
}
});
});
5.?springboot采用MultipartFile接收,直接轉(zhuǎn)換成輸入流讀取即可,以前我做的時候不懂先生成文件,再讀取文件成輸入流,如下:BufferedInputStream?fileStream?=?new?BufferedInputStream(file.getInputStream());
6.?代碼如下:@PostMapping("/importExcel")
@ResponseBody
public?ResultBean?importExcel(@RequestParam("file")?MultipartFile?file)?{
ResultBean?resultBean?=?new?ResultBean();
try?{
BufferedInputStream?fileStream?=?new?BufferedInputStream(file.getInputStream());
Sheet?sheet?=?new?Sheet(1,?0);
List?list?=?EasyExcelFactory.read(fileStream,?sheet);
for?(Object?obj?:?list)?{
System.out.println(obj);
}
}?catch?(Exception?e)?{
e.printStackTrace();
}
return?resultBean;
}
7.?當然easyexcel需要引入依賴:
com.alibaba
easyexcel
1.1.2-beat1
8.excel模板:
9.解析結(jié)果:
總結(jié)
以上是生活随笔為你收集整理的springboot 上传文件解析入库_SpringBoot + easyexcel + WebUploader 实现文件上传并解析的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: (二分搜索法尺取法)subsequenc
- 下一篇: mysql dnslog_dnslog小