1.前端使用fileinput插件上传
$("#file2").fileinput({
language: 'zh',
uploadUrl: basePath + 'uploadVehicleFile',
maxFileCount: 40, //表示允许同时上传的最大文件个数
maxFileSize: 20480000,
showCaption: true, //是否显示标题
browseClass: "btn btn-primary", //按钮样式
showPreview: true,
allowedFileExtensions: ["xls","xlsx"],
uploadAsync: true,
showUpload: true, //是否显示上传按钮
maxImageWidth: 80,
maxImageHeight: 80,
enctype: 'multipart/form-data', //不对发送的字符进行编码
previewFileIcon: "",
msgFilesTooMany: "选择上传的文件数量({n}) 超过允许的最大数值{m}!",
uploadExtraData: function(previewId, index) {
var data = {
bidId: null,
planProId: "项目主键",
belongProject: "所属项目",
firstMenu: "一级菜单",
secondMenu: "二级菜单",
fileType: "文件类型",
workingstageId: "工作空间",
parentTreeId: "父类树id"
};
return data;
},
previewFileIconSettings: {
'doc': '',
'xls': '',
'ppt': '',
'jpg': '',
'pdf': '',
'zip': '',
'htm': '',
'txt': '',
'mov': '',
'mp3': '',
},
previewFileExtSettings: {
'doc': function(ext) {
return ext.match(/(doc|docx)$/i);
},
'xls': function(ext) {
return ext.match(/(xls|xlsx)$/i);
},
'ppt': function(ext) {
return ext.match(/(ppt|pptx)$/i);
},
'zip': function(ext) {
return ext.match(/(zip|rar|tar|gzip|gz|7z)$/i);
},
'htm': function(ext) {
return ext.match(/(php|js|css|htm|html)$/i);
},
'txt': function(ext) {
return ext.match(/(txt|ini|md)$/i);
},
'mov': function(ext) {
return ext.match(/(avi|mpg|mkv|mov|mp4|3gp|webm|wmv)$/i);
},
'mp3': function(ext) {
return ext.match(/(mp3|wav)$/i);
},
}
});
2.后端接收,使用springmvc方式
controller层
@RequestMapping("/uploadVehicleFile")
public String uploadAndParseRoadConfirmFile(@RequestParam("file") MultipartFile[] files, HttpServletRequest request,HttpServletResponse response) {
try {
String planProId = request.getParameter("planProId");
if(examineFileListService.chooseProId(planProId)) {
return robPlandRoadConfirmService.uploadAndParseRoadConfirmFile(files,request,response);
}else {
return new Message(100,"文件上传解析失败,请选择所属项目!").toJson();
}
} catch (Exception e) {
return new Message(Message.ERROR,"文件上传有误!").toJson();
}
}
service层:
public String uploadVehicleFile(MultipartFile[] files, HttpServletRequest request, HttpServletResponse response) {
Message message = new Message();
try {
if(files!=null && files.length>0){
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
List robPlancqCplanList = new ArrayList();
for (int i = 0; i < files.length; i++) {
robPlancqCplanList.clear();
MultipartFile file = files[i];
String md5 = DigestUtils.md5Hex(file.getInputStream()); //这里不能用fileInput,试了好久
String filePath=examineFileListService.getSaveMinioPath(request); //保存在minio中的目录结构
String fileName = file.getOriginalFilename(); //文件名字
String hql = "select * from 文件表 where file_md5='"+md5+"' and file_path='"+filePath+"'"; //验证文件是否存在,不存在再上传
List existExamineFileList = super.getDao().findBySql(hql, ExamineFileList.class);
if(existExamineFileList != null && existExamineFileList.size()>0) { //指定目录下已经存在此文件,不进行上传操作
message.setType(100);
message.setMsg(fileName+"已经上传过,不再重复上传,文件上传失败");
}else{
InputStream fileInput = file.getInputStream(); //输入流
Workbook workBook = examineFileListService.getWorkBook(fileInput,fileName);
if(workBook != null){
String planProId = request.getParameter("planProId");
message = parseFileAndSave(workBook,robPlancqCplanList,planProId,fileName); //解析和保存文件
if(message.getType()==200){ //解析没有问题才上传到Minio中和保存到数据库中
robPlancqCplanRepository.save(robPlancqCplanList);
message = examineFileListService.uploadFileOne(file,request,response);
}
}else {
message.setType(100);
message.setMsg(fileName+"为空,文件上传失败");
}
}
}
}else {
message.setType(100);
message.setMsg("上传文件数量少于1,文件上传失败");
}
} catch (Exception e) {
message.setType(Message.ERROR);
message.setMsg("请检测excel必填项目完善文档,文件上传失败");
}
return message.toJson();
}
/**
* @Description 解析和保存文件
* @author [zhanglizeng] Tel:
* @createDate 2018年11月22日 下午3:21:04
*/
private Message parseFileAndSave(Workbook workBook, List robPlancqCplanList, String planProId,String fileName) {
Message message = new Message();
try {
for(int sheetNum=0;sheetNum< workBook.getNumberOfSheets();sheetNum++){
Sheet sheet = workBook.getSheetAt(sheetNum);
if(sheet.getLastRowNum() == 0 && sheet.getPhysicalNumberOfRows() == 0){
continue;
}
message = parseSheet(sheet,robPlancqCplanList,planProId,fileName);
if(message.getType() != 200){ //有一个sheet不符合判断此解析文档无效
return message;
}
}
} catch (Exception e) {
return new Message(Message.ERROR,fileName+"文件解析失败");
}
return message;
}
/**
* @Description 解析sheet
* @author [zhanglizeng] Tel:
* @createDate 2018年11月22日 下午3:23:09
*/
private Message parseSheet(Sheet sheet, List robPlancqCplanList, String planProId,String fileName) {
Message message = new Message();
try {
String title = examineFileListService.parseCell(sheet.getRow(0).getCell(0)); //得到标题
if(title != null && title.length()>0){ //判断标题是否合法
if(checkColTitle(sheet)){ //判断excel的列标题是否和模板一致
message = parseCell(sheet,robPlancqCplanList,planProId,fileName); //解析文件
}else{
message.setType(100);
message.setMsg("文件中的列标题顺序不符合要求,请参照模板填写,"+fileName+"解析失败!");
}
}else {
return new Message(100,"sheet没有标题,"+fileName+"文件解析失败");
}
} catch (Exception e) {
return new Message(Message.ERROR,"存在为空的sheet,建议删除空sheet,文件解析失败");
}
return message;
}
/**
* @Description 解析文件
* @author [zhanglizeng] Tel:
* @createDate 2018年11月22日 下午3:43:34
*/
private Message parseCell(Sheet sheet, List robPlancqCplanList, String planProId, String fileName) {
int temp=0;
try {
for(int rowNum = 2;rowNum <= sheet.getPhysicalNumberOfRows();++rowNum){
Row row = sheet.getRow(rowNum);
if(row == null){
continue;
}
temp = rowNum;
parseRow(row,robPlancqCplanList,planProId); //解析每一行数据
}
} catch (Exception e) {
return new Message(Message.ERROR,(temp+1)+"行文本内容不符合模板要求,"+fileName+"解析失败");
}
return new Message(Message.OK,fileName+"文件上传成功");
}
/**
* @Description 解析每一行数据
* @author [zhanglizeng] Tel:
* @createDate 2018年11月22日 下午3:45:33
*/
private void parseRow(Row row, List robPlancqCplanList, String planProId)throws Exception {
String carType = examineFileListService.parseCell(row.getCell(0)).trim();
String carUnit = examineFileListService.parseCell(row.getCell(1)).trim();
String carPlates = examineFileListService.parseCell(row.getCell(2)).trim();
String carVtime = examineFileListService.parseCell(row.getCell(3)).trim();
String carRemark = examineFileListService.parseCell(row.getCell(4)).trim();
if((carType!=null && carType.length()>0) || (carUnit!=null && carUnit.length()>0) ||
(carPlates!=null && carPlates.length()>0) || (carVtime!=null && carVtime.length()>0)
|| (carRemark!=null && carRemark.length()>0)) {
RobPlancqCplan robPlancqCplan = new RobPlancqCplan();
robPlancqCplan.setCarType(carType);
robPlancqCplan.setCarUnit(carUnit);
robPlancqCplan.setCarPlates(carPlates);
if(carVtime != null && carVtime.length()>0) {
robPlancqCplan.setCarVtime(examineFileListService.getDateByString(carVtime)); //转成日期格式
}
robPlancqCplan.setCarRemark(carRemark);
robPlancqCplan.setCreateTime(new Date());
robPlancqCplan.setCreateUserCode(getUserCode());
robPlancqCplan.setCreateUserName(getUserName());
robPlancqCplan.setGuid(getUUID());
robPlancqCplan.setPlanProId(planProId);
robPlancqCplanList.add(robPlancqCplan);
}
}
/**
* @Description 判断excel的列标题是否和模板一致
* @author [zhanglizeng] Tel:
* @createDate 2018年11月22日 下午3:25:28
*/
private boolean checkColTitle(Sheet sheet) {
String carType = examineFileListService.parseCell(sheet.getRow(1).getCell(0)).trim(); //车辆用途
String carUnit = examineFileListService.parseCell(sheet.getRow(1).getCell(1)).trim(); //车辆单位
String carPlates = examineFileListService.parseCell(sheet.getRow(1).getCell(2)).trim(); //车辆牌照
String carVtime = examineFileListService.parseCell(sheet.getRow(1).getCell(3)).trim(); //校验完成时间
String carRemark = examineFileListService.parseCell(sheet.getRow(1).getCell(4)).trim(); //备注
if("车辆用途".equals(carType) && "车辆单位".equals(carUnit) && "车辆牌照".equals(carPlates)
&& "校验完成时间".equals(carVtime) && "备注".equals(carRemark)) {
return true;
}else {
return false;
}
}
3.页面接收上传结果:
$("#file2").on('fileuploaded', function(event, data, previewId, index) {
var message = mini.decode(data);
if(message.response.type == 200) {
if(!flag2){ //手动点击上传
showMsg("xx文件上传成功","success",800,"center","center");
}else{ //点击确定时检测到没有上传,采取的上传操作
if(getFileTotal2()==1){
if(getFileTotal1()==0 && getFileTotal3()==0){ //其它文件都上传完了,可以在个方法中关闭窗口
showMsg("xx上传成功","success",1800,"center","center",closeWindow);
}else{
showMsg("xx件上传成功","success",800,"center","center");
}
}
}
}else if(message.response.type == 150){
showMsg("请选择所属项目再上传xx文件,文件上传失败","warning",2000,"center","center");
}else {
showMsg(message.response.msg, "warning", 1200, "center", "center");
}
});