|
@@ -1,24 +1,31 @@
|
|
package com.qmth.teachcloud.common.util;
|
|
package com.qmth.teachcloud.common.util;
|
|
|
|
|
|
|
|
+import com.qmth.boot.tools.excel.ExcelReader;
|
|
|
|
+import com.qmth.boot.tools.excel.annotation.ExcelColumn;
|
|
|
|
+import com.qmth.boot.tools.excel.enums.ExcelType;
|
|
|
|
+import com.qmth.teachcloud.common.annotation.ExcelError;
|
|
|
|
+import com.qmth.teachcloud.common.bean.result.ExcelResult;
|
|
import com.qmth.teachcloud.common.contant.SystemConstant;
|
|
import com.qmth.teachcloud.common.contant.SystemConstant;
|
|
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
|
|
import com.qmth.teachcloud.common.enums.ExceptionResultEnum;
|
|
|
|
+import org.apache.commons.collections4.CollectionUtils;
|
|
import org.apache.commons.io.IOUtils;
|
|
import org.apache.commons.io.IOUtils;
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
import org.apache.pdfbox.pdmodel.PDDocument;
|
|
import org.apache.pdfbox.pdmodel.PDPageTree;
|
|
import org.apache.pdfbox.pdmodel.PDPageTree;
|
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
|
import org.apache.pdfbox.rendering.PDFRenderer;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.slf4j.LoggerFactory;
|
|
-import org.springframework.data.redis.core.RedisTemplate;
|
|
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
-import javax.annotation.Resource;
|
|
|
|
import javax.imageio.ImageIO;
|
|
import javax.imageio.ImageIO;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import javax.servlet.http.HttpServletResponse;
|
|
import java.awt.image.BufferedImage;
|
|
import java.awt.image.BufferedImage;
|
|
import java.io.*;
|
|
import java.io.*;
|
|
|
|
+import java.lang.reflect.Field;
|
|
import java.net.URLEncoder;
|
|
import java.net.URLEncoder;
|
|
import java.text.SimpleDateFormat;
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.*;
|
|
import java.util.*;
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
|
/**
|
|
/**
|
|
* @Description: 转换工具类
|
|
* @Description: 转换工具类
|
|
@@ -28,28 +35,8 @@ import java.util.*;
|
|
|
|
|
|
@Component
|
|
@Component
|
|
public class ConvertUtil {
|
|
public class ConvertUtil {
|
|
- private final static Logger log = LoggerFactory.getLogger(ConvertUtil.class);
|
|
|
|
-
|
|
|
|
- @Resource
|
|
|
|
- RedisTemplate<String, Object> redisTemplate;
|
|
|
|
-
|
|
|
|
- // inputStream转outputStream
|
|
|
|
- public static ByteArrayOutputStream parse(final InputStream in) throws Exception {
|
|
|
|
- final ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
|
|
|
|
-// int ch;
|
|
|
|
-// while ((ch = in.read()) != -1) {
|
|
|
|
-// swapStream.write(ch);
|
|
|
|
-// }
|
|
|
|
- IOUtils.copy(in, swapStream);
|
|
|
|
- return swapStream;
|
|
|
|
- }
|
|
|
|
|
|
|
|
- // outputStream转inputStream
|
|
|
|
- public static ByteArrayInputStream parse(final OutputStream out) {
|
|
|
|
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
|
|
- baos = (ByteArrayOutputStream) out;
|
|
|
|
- return new ByteArrayInputStream(baos.toByteArray());
|
|
|
|
- }
|
|
|
|
|
|
+ private final static Logger log = LoggerFactory.getLogger(ConvertUtil.class);
|
|
|
|
|
|
/**
|
|
/**
|
|
* 转换,解析时间
|
|
* 转换,解析时间
|
|
@@ -68,7 +55,8 @@ public class ConvertUtil {
|
|
String startTimeStr = date + " " + arr[0];
|
|
String startTimeStr = date + " " + arr[0];
|
|
String endTimeStr = date + " " + arr[1];
|
|
String endTimeStr = date + " " + arr[1];
|
|
if (Objects.isNull(DateDisposeUtils.parseDate(startTimeStr)) || Objects.isNull(DateDisposeUtils.parseDate(endTimeStr))) {
|
|
if (Objects.isNull(DateDisposeUtils.parseDate(startTimeStr)) || Objects.isNull(DateDisposeUtils.parseDate(endTimeStr))) {
|
|
- throw ExceptionResultEnum.ERROR.exception("提供的日期时间格式异常 \n考试日期[" + date + "]应该使用如下的格式(考试日期:2021-09-01 或 2021/09/01 或 2021.09.01)" + "\n考试时间[" + time + "]应该使用如下的格式(考试时间:18:00-20:00)");
|
|
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception(
|
|
|
|
+ "提供的日期时间格式异常 \n考试日期[" + date + "]应该使用如下的格式(考试日期:2021-09-01 或 2021/09/01 或 2021.09.01)" + "\n考试时间[" + time + "]应该使用如下的格式(考试时间:18:00-20:00)");
|
|
}
|
|
}
|
|
long startTime = DateDisposeUtils.parseDate(startTimeStr).getTime();
|
|
long startTime = DateDisposeUtils.parseDate(startTimeStr).getTime();
|
|
long endTime = DateDisposeUtils.parseDate(endTimeStr).getTime();
|
|
long endTime = DateDisposeUtils.parseDate(endTimeStr).getTime();
|
|
@@ -97,7 +85,6 @@ public class ConvertUtil {
|
|
String date;
|
|
String date;
|
|
String time;
|
|
String time;
|
|
|
|
|
|
-
|
|
|
|
Map<String, Object> map = new HashMap<>();
|
|
Map<String, Object> map = new HashMap<>();
|
|
|
|
|
|
Calendar calendar = Calendar.getInstance();
|
|
Calendar calendar = Calendar.getInstance();
|
|
@@ -121,7 +108,6 @@ public class ConvertUtil {
|
|
return map;
|
|
return map;
|
|
}
|
|
}
|
|
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 根据开始时间和结束时间解析日期和时间
|
|
* 根据开始时间和结束时间解析日期和时间
|
|
*
|
|
*
|
|
@@ -148,6 +134,81 @@ public class ConvertUtil {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 解析excel
|
|
|
|
+ *
|
|
|
|
+ * @param inputStream inputStream
|
|
|
|
+ * @param clazz 实体类映射对象
|
|
|
|
+ * @param fillError 是否为映射对象添加错误信息(如果要添加的话映射对象的属性必须有一个字段有@ExcelError注解)
|
|
|
|
+ * @param columnNameRow 从第几行开始
|
|
|
|
+ * @throws Exception 异常
|
|
|
|
+ */
|
|
|
|
+ public static <T> ExcelResult<T> analyzeExcel(InputStream inputStream, Class<T> clazz, boolean fillError, int columnNameRow)
|
|
|
|
+ throws Exception {
|
|
|
|
+ boolean analyzeResult = true;
|
|
|
|
+ ExcelReader excelReader = ExcelReader.create(ExcelType.XLSX, inputStream, columnNameRow);
|
|
|
|
+ List<String[]> excelDatasource = excelReader.getDataArrayList();
|
|
|
|
+ List<T> dtoList = new ArrayList<>();
|
|
|
|
+ if (CollectionUtils.isNotEmpty(excelDatasource)) {
|
|
|
|
+ List<String> columnNameList = Arrays.asList(excelReader.getColumnNames());
|
|
|
|
+ Field[] fields = clazz.getDeclaredFields();
|
|
|
|
+ Field errorField = null;
|
|
|
|
+ if (fillError) {
|
|
|
|
+ for (Field field : fields) {
|
|
|
|
+ ExcelError annotation = field.getAnnotation(ExcelError.class);
|
|
|
|
+ if (Objects.nonNull(annotation)) {
|
|
|
|
+ errorField = field;
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ // 要导入的字段
|
|
|
|
+ List<Field> fieldList = Arrays.stream(fields).filter(field -> {
|
|
|
|
+ ExcelColumn annotation = field.getAnnotation(ExcelColumn.class);
|
|
|
|
+ return Objects.nonNull(annotation);
|
|
|
|
+ }).collect(Collectors.toList());
|
|
|
|
+ List<String> fieldNameList = fieldList.stream().map(e -> e.getAnnotation(ExcelColumn.class).name()).collect(Collectors.toList());
|
|
|
|
+
|
|
|
|
+ if (columnNameList.size() != fieldNameList.size()) {
|
|
|
|
+ throw ExceptionResultEnum.ERROR.exception(String.format("表头错误,应为[%s]", String.join(";", fieldNameList)));
|
|
|
|
+ }
|
|
|
|
+ for (String[] cell : excelDatasource) {
|
|
|
|
+ T dto = clazz.newInstance();
|
|
|
|
+ boolean emptyCell = true;
|
|
|
|
+ List<String> excelErrorList = new ArrayList<>();
|
|
|
|
+ for (int i = 0; i < cell.length; i++) {
|
|
|
|
+ String v = StringUtils.trimToNull(cell[i]);
|
|
|
|
+ Field field = fieldList.get(i);
|
|
|
|
+ ExcelColumn annotation = field.getAnnotation(ExcelColumn.class);
|
|
|
|
+ if (SystemConstant.strNotNull(v)) {
|
|
|
|
+ field.setAccessible(true);
|
|
|
|
+ field.set(dto, v);
|
|
|
|
+ emptyCell = false;
|
|
|
|
+ } else if (annotation.nullable()) {
|
|
|
|
+ excelErrorList.add(String.format("[%s]不能为空", annotation.name()));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (emptyCell) {
|
|
|
|
+ // 舍弃全空行
|
|
|
|
+ continue;
|
|
|
|
+ }
|
|
|
|
+ if (CollectionUtils.isNotEmpty(excelErrorList)) {
|
|
|
|
+ // 有异常数据
|
|
|
|
+ analyzeResult = false;
|
|
|
|
+ if (fillError && Objects.nonNull(errorField)) {
|
|
|
|
+ errorField.setAccessible(true);
|
|
|
|
+ errorField.set(dto, String.join(",", excelErrorList));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ dtoList.add(dto);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ ExcelResult<T> excelResult = new ExcelResult<>();
|
|
|
|
+ excelResult.setSuccess(analyzeResult);
|
|
|
|
+ excelResult.setDatasource(dtoList);
|
|
|
|
+ return excelResult;
|
|
|
|
+ }
|
|
|
|
+
|
|
public static void delFolder(String folderPath) {
|
|
public static void delFolder(String folderPath) {
|
|
try {
|
|
try {
|
|
delAllFile(folderPath); //删除完里面所有内容
|
|
delAllFile(folderPath); //删除完里面所有内容
|
|
@@ -190,8 +251,8 @@ public class ConvertUtil {
|
|
|
|
|
|
public static void outputFile(HttpServletResponse response, InputStream inputStream, String fileName) {
|
|
public static void outputFile(HttpServletResponse response, InputStream inputStream, String fileName) {
|
|
try {
|
|
try {
|
|
-// byte[] buf = new byte[1024];
|
|
|
|
-// int len = 0;
|
|
|
|
|
|
+ // byte[] buf = new byte[1024];
|
|
|
|
+ // int len = 0;
|
|
|
|
|
|
String fName = new String(fileName.getBytes(), "ISO-8859-1");
|
|
String fName = new String(fileName.getBytes(), "ISO-8859-1");
|
|
|
|
|
|
@@ -199,15 +260,15 @@ public class ConvertUtil {
|
|
response.setContentType("application/x-msdownload");
|
|
response.setContentType("application/x-msdownload");
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + fName);
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + fName);
|
|
|
|
|
|
-// OutputStream outStream = response.getOutputStream();
|
|
|
|
|
|
+ // OutputStream outStream = response.getOutputStream();
|
|
|
|
|
|
-// while ((len = inputStream.read(buf)) > 0) {
|
|
|
|
-// outStream.write(buf, 0, len);
|
|
|
|
-// }
|
|
|
|
|
|
+ // while ((len = inputStream.read(buf)) > 0) {
|
|
|
|
+ // outStream.write(buf, 0, len);
|
|
|
|
+ // }
|
|
|
|
|
|
IOUtils.copy(inputStream, response.getOutputStream());
|
|
IOUtils.copy(inputStream, response.getOutputStream());
|
|
inputStream.close();
|
|
inputStream.close();
|
|
-// outStream.close();
|
|
|
|
|
|
+ // outStream.close();
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
log.error(SystemConstant.LOG_ERROR, e);
|
|
log.error(SystemConstant.LOG_ERROR, e);
|
|
}
|
|
}
|
|
@@ -221,9 +282,9 @@ public class ConvertUtil {
|
|
response.setContentType("application/x-msdownload");
|
|
response.setContentType("application/x-msdownload");
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + fName);
|
|
response.setHeader("Content-Disposition", "attachment; filename=" + fName);
|
|
|
|
|
|
-// OutputStream outStream = response.getOutputStream();
|
|
|
|
-// baos.writeTo(outStream);
|
|
|
|
-// outStream.close();
|
|
|
|
|
|
+ // OutputStream outStream = response.getOutputStream();
|
|
|
|
+ // baos.writeTo(outStream);
|
|
|
|
+ // outStream.close();
|
|
IOUtils.write(baos.toByteArray(), response.getOutputStream());
|
|
IOUtils.write(baos.toByteArray(), response.getOutputStream());
|
|
} catch (IOException e) {
|
|
} catch (IOException e) {
|
|
throw new Exception("下载失败");
|
|
throw new Exception("下载失败");
|
|
@@ -234,7 +295,7 @@ public class ConvertUtil {
|
|
PDDocument doc = null;
|
|
PDDocument doc = null;
|
|
ByteArrayOutputStream os = null;
|
|
ByteArrayOutputStream os = null;
|
|
InputStream stream = null;
|
|
InputStream stream = null;
|
|
-// OutputStream out = null;
|
|
|
|
|
|
+ // OutputStream out = null;
|
|
List<File> fileList = new ArrayList<>();
|
|
List<File> fileList = new ArrayList<>();
|
|
try {
|
|
try {
|
|
// pdf路径
|
|
// pdf路径
|
|
@@ -255,9 +316,9 @@ public class ConvertUtil {
|
|
file.getParentFile().mkdirs();
|
|
file.getParentFile().mkdirs();
|
|
file.createNewFile();
|
|
file.createNewFile();
|
|
}
|
|
}
|
|
-// out = new FileOutputStream(file);
|
|
|
|
|
|
+ // out = new FileOutputStream(file);
|
|
IOUtils.write(os.toByteArray(), new FileOutputStream(file));
|
|
IOUtils.write(os.toByteArray(), new FileOutputStream(file));
|
|
-// out.write(dataList);
|
|
|
|
|
|
+ // out.write(dataList);
|
|
fileList.add(file);
|
|
fileList.add(file);
|
|
}
|
|
}
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
@@ -320,5 +381,4 @@ public class ConvertUtil {
|
|
}
|
|
}
|
|
return fileList;
|
|
return fileList;
|
|
}
|
|
}
|
|
-
|
|
|
|
}
|
|
}
|