|
@@ -2,14 +2,18 @@ package com.mooctest.service;
|
|
|
|
|
|
import com.mooctest.dao.ExtendBugDao;
|
|
import com.mooctest.dao.ExtendBugDao;
|
|
import com.mooctest.dao.TaskDao;
|
|
import com.mooctest.dao.TaskDao;
|
|
|
|
+import com.mooctest.event.EventUtil;
|
|
import com.mooctest.model.ExtendBug;
|
|
import com.mooctest.model.ExtendBug;
|
|
import com.mooctest.model.Task;
|
|
import com.mooctest.model.Task;
|
|
|
|
+import org.apache.commons.io.IOUtils;
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
import org.apache.poi.ss.usermodel.Cell;
|
|
import org.apache.poi.ss.usermodel.Cell;
|
|
import org.apache.poi.ss.usermodel.Row;
|
|
import org.apache.poi.ss.usermodel.Row;
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
import org.apache.poi.ss.usermodel.Sheet;
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
import org.apache.poi.ss.usermodel.Workbook;
|
|
import org.bson.Document;
|
|
import org.bson.Document;
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
@@ -20,6 +24,9 @@ import java.util.*;
|
|
@Service
|
|
@Service
|
|
public class ExcelInputService {
|
|
public class ExcelInputService {
|
|
|
|
|
|
|
|
+ private static Logger logger = LoggerFactory.getLogger(EventUtil.class);
|
|
|
|
+
|
|
|
|
+
|
|
@Autowired
|
|
@Autowired
|
|
TaskDao taskDao;
|
|
TaskDao taskDao;
|
|
|
|
|
|
@@ -29,19 +36,11 @@ public class ExcelInputService {
|
|
// 接收前端文件,并将MultipartFile文件转换成File文件,并返回文件路径
|
|
// 接收前端文件,并将MultipartFile文件转换成File文件,并返回文件路径
|
|
public String saveMultipartFile(MultipartFile file, String fileSaveName){
|
|
public String saveMultipartFile(MultipartFile file, String fileSaveName){
|
|
OutputStream os = null;
|
|
OutputStream os = null;
|
|
- InputStream inputStream = null;
|
|
|
|
- String fileName = fileSaveName;
|
|
|
|
String fileFullPath = null;
|
|
String fileFullPath = null;
|
|
- String fileType =null;
|
|
|
|
-
|
|
|
|
- try {
|
|
|
|
- inputStream = file.getInputStream();
|
|
|
|
- fileName = file.getOriginalFilename();
|
|
|
|
-
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }
|
|
|
|
- try {
|
|
|
|
|
|
+ String fileName = file.getOriginalFilename();
|
|
|
|
+ try (
|
|
|
|
+ InputStream inputStream = file.getInputStream();
|
|
|
|
+ ) {
|
|
String path = System.getProperty("user.dir");
|
|
String path = System.getProperty("user.dir");
|
|
// 2、保存到临时文件
|
|
// 2、保存到临时文件
|
|
// 1K的数据缓冲
|
|
// 1K的数据缓冲
|
|
@@ -59,21 +58,12 @@ public class ExcelInputService {
|
|
while ((len = inputStream.read(bs)) != -1) {
|
|
while ((len = inputStream.read(bs)) != -1) {
|
|
os.write(bs, 0, len);
|
|
os.write(bs, 0, len);
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
- } finally {
|
|
|
|
- // 完毕,关闭所有链接
|
|
|
|
- try {
|
|
|
|
- os.close();
|
|
|
|
- inputStream.close();
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
|
|
+ }finally {
|
|
|
|
+ IOUtils.closeQuietly(os);
|
|
}
|
|
}
|
|
return fileFullPath;
|
|
return fileFullPath;
|
|
}
|
|
}
|
|
@@ -95,12 +85,8 @@ public class ExcelInputService {
|
|
// 输入文件
|
|
// 输入文件
|
|
FileInputStream inputStream = new FileInputStream(address);
|
|
FileInputStream inputStream = new FileInputStream(address);
|
|
// 根据输入流导入Excel产生Workbook对象
|
|
// 根据输入流导入Excel产生Workbook对象
|
|
- Workbook workbook = null;
|
|
|
|
- try {
|
|
|
|
- workbook = new HSSFWorkbook(inputStream);
|
|
|
|
- } catch (IOException e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }
|
|
|
|
|
|
+ Workbook workbook = new HSSFWorkbook(inputStream);
|
|
|
|
+
|
|
|
|
|
|
fieldList = new ArrayList<String>();
|
|
fieldList = new ArrayList<String>();
|
|
// 获取Excel文档中第一个表单
|
|
// 获取Excel文档中第一个表单
|
|
@@ -119,7 +105,7 @@ public class ExcelInputService {
|
|
fieldList.add(cell.toString());
|
|
fieldList.add(cell.toString());
|
|
}
|
|
}
|
|
|
|
|
|
- System.out.println("文件列表" + fieldList);
|
|
|
|
|
|
+ logger.info("文件列表" + fieldList);
|
|
// 获得表单的行数
|
|
// 获得表单的行数
|
|
int rows = sheet.getLastRowNum() + 1;
|
|
int rows = sheet.getLastRowNum() + 1;
|
|
|
|
|
|
@@ -150,7 +136,7 @@ public class ExcelInputService {
|
|
if(topName.equals("title") || topName.equals("题目")){
|
|
if(topName.equals("title") || topName.equals("题目")){
|
|
String title;
|
|
String title;
|
|
if(cell == null || cell.toString().equals("")){
|
|
if(cell == null || cell.toString().equals("")){
|
|
- title = "null";
|
|
|
|
|
|
+ title = "空";
|
|
}else{
|
|
}else{
|
|
title = cell.toString();
|
|
title = cell.toString();
|
|
}
|
|
}
|
|
@@ -158,7 +144,7 @@ public class ExcelInputService {
|
|
}else if(topName.equals("bug_category") || topName.equals("分类")){
|
|
}else if(topName.equals("bug_category") || topName.equals("分类")){
|
|
String bug_category;
|
|
String bug_category;
|
|
if(cell == null || cell.toString().equals("")){
|
|
if(cell == null || cell.toString().equals("")){
|
|
- bug_category = "null";
|
|
|
|
|
|
+ bug_category = "空";
|
|
}else{
|
|
}else{
|
|
bug_category = cell.toString();
|
|
bug_category = cell.toString();
|
|
}
|
|
}
|
|
@@ -184,7 +170,7 @@ public class ExcelInputService {
|
|
}else{
|
|
}else{
|
|
String description;
|
|
String description;
|
|
if(cell == null || cell.toString().equals("")){
|
|
if(cell == null || cell.toString().equals("")){
|
|
- description = "null";
|
|
|
|
|
|
+ description = "空";
|
|
}else{
|
|
}else{
|
|
description = cell.toString();
|
|
description = cell.toString();
|
|
}
|
|
}
|
|
@@ -196,14 +182,19 @@ public class ExcelInputService {
|
|
System.out.println(extendBug);
|
|
System.out.println(extendBug);
|
|
extendBugDao.save(extendBug);
|
|
extendBugDao.save(extendBug);
|
|
}
|
|
}
|
|
- System.out.println("插入成功");
|
|
|
|
|
|
+ logger.info("文件导入成功!");
|
|
// 导入成功后将本地文件夹删除
|
|
// 导入成功后将本地文件夹删除
|
|
File file = new File(address);
|
|
File file = new File(address);
|
|
file.delete();
|
|
file.delete();
|
|
insert_map.put("insert_success", true);
|
|
insert_map.put("insert_success", true);
|
|
return insert_map;
|
|
return insert_map;
|
|
} catch (FileNotFoundException e) {
|
|
} catch (FileNotFoundException e) {
|
|
- System.err.println(e.getClass().getName() + ": " + e.getMessage());
|
|
|
|
|
|
+ logger.info(e.getMessage());
|
|
|
|
+ insert_map.put("insert_success", false);
|
|
|
|
+ return insert_map;
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ logger.info(e.getMessage());
|
|
|
|
+ insert_map.put("file_is_null", true);
|
|
insert_map.put("insert_success", false);
|
|
insert_map.put("insert_success", false);
|
|
return insert_map;
|
|
return insert_map;
|
|
}
|
|
}
|