修改解析文件数据

master
bynt 3 years ago
parent 9a9f4158fe
commit 3d08d302d9

@ -40,9 +40,13 @@ public class DefaultConstant {
public static final int TEN_NUMBER = 10; public static final int TEN_NUMBER = 10;
/** /**
* 10 * 11
*/ */
public static final int ELEVEN_NUMBER = 11; public static final int ELEVEN_NUMBER = 11;
/**
* 12
*/
public static final int TWELVE_NUMBER = 12;
/** /**

@ -2,6 +2,7 @@ package me.zhengjie.modules.constant;
/** /**
* @author
* *
*/ */
public class FileConstant { public class FileConstant {
@ -16,6 +17,21 @@ public class FileConstant {
*/ */
public static final String XLSX_FILE_SUB_NAME = "xlsx"; public static final String XLSX_FILE_SUB_NAME = "xlsx";
/**
* xls
*/
public static final String XLS_FILE_SUB_NAME = "xls";
/**
* txt
*/
public static final String TXT_FILE_SUB_NAME = "txt";
/**
* txt
*/
public static final String CSV_FILE_SUB_NAME = "csv";
/** /**
* zip * zip

@ -73,6 +73,10 @@ public class SmsConfiguration implements Serializable {
@ApiModelProperty(value = "更新时间") @ApiModelProperty(value = "更新时间")
private Timestamp updateTime; private Timestamp updateTime;
@Column(name = "middle_image")
@ApiModelProperty(value = "中间页图片")
private String middleImage;
public void copy(SmsConfiguration source){ public void copy(SmsConfiguration source){
BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true)); BeanUtil.copyProperties(source,this, CopyOptions.create().setIgnoreNullValue(true));
} }

@ -53,6 +53,9 @@ public class SmsConfigurationDto implements Serializable {
/** 背景图片 */ /** 背景图片 */
private String imageUrl; private String imageUrl;
/** 中间页图片 */
private String middleImage;
/** 创建日期 */ /** 创建日期 */
private Timestamp createTime; private Timestamp createTime;

@ -25,6 +25,7 @@ import me.zhengjie.annotation.rest.RedisLock;
import me.zhengjie.common.http.ResponseCode; import me.zhengjie.common.http.ResponseCode;
import me.zhengjie.exception.BadRequestException; import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.constant.DefaultConstant; import me.zhengjie.modules.constant.DefaultConstant;
import me.zhengjie.modules.constant.FileConstant;
import me.zhengjie.modules.sms.domain.SmsConfiguration; import me.zhengjie.modules.sms.domain.SmsConfiguration;
import me.zhengjie.modules.sms.repository.SmsConfigurationRepository; import me.zhengjie.modules.sms.repository.SmsConfigurationRepository;
import me.zhengjie.modules.sms.service.SmsConfigurationService; import me.zhengjie.modules.sms.service.SmsConfigurationService;
@ -144,13 +145,13 @@ public class SmsConfigurationServiceImpl implements SmsConfigurationService {
String name = file.getOriginalFilename(); String name = file.getOriginalFilename();
String taskName = messageVo.getTaskName(); String taskName = messageVo.getTaskName();
if (name != null) { if (name != null) {
int lastIndexOf = name.lastIndexOf(StrUtil.DOT); int lastIndexOf = name.lastIndexOf(StrUtil.DOT) + DefaultConstant.ONE_NUMBER;
// 校验文件格式 // 校验文件格式
String nameStr = name.substring(lastIndexOf); String nameStr = name.substring(lastIndexOf);
if (!((".xlsx".equals(nameStr) if (!((FileConstant.XLSX_FILE_SUB_NAME.equals(nameStr)
|| ".xls".equals(nameStr)) || FileConstant.XLS_FILE_SUB_NAME.equals(nameStr))
|| ".txt".equals(nameStr) || FileConstant.TXT_FILE_SUB_NAME.equals(nameStr)
|| ".csv".equals(nameStr))) { || FileConstant.CSV_FILE_SUB_NAME.equals(nameStr))) {
throw new BadRequestException(ResponseCode.NO_FILE_FORMAT.getDesc()); throw new BadRequestException(ResponseCode.NO_FILE_FORMAT.getDesc());
} }
// 任务名称检验,为必填参数,且不能重复 // 任务名称检验,为必填参数,且不能重复

@ -27,8 +27,10 @@ public class UploadAndSendMessageVo {
@ApiModelProperty(value = "图片路径") @ApiModelProperty(value = "图片路径")
private String picturePath; private String picturePath;
@ApiModelProperty(value = "跳转链接") @ApiModelProperty(value = "跳转链接")
private String linkAddress; private String linkAddress;
@ApiModelProperty(value = "中间页链接")
private String middleImage;
} }

@ -15,12 +15,15 @@
*/ */
package me.zhengjie.modules.uploadnew.service.impl; package me.zhengjie.modules.uploadnew.service.impl;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
import cn.hutool.system.OsInfo; import cn.hutool.system.OsInfo;
import cn.hutool.system.SystemUtil; import cn.hutool.system.SystemUtil;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.zhengjie.common.http.ResponseCode;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.constant.DefaultConstant;
import me.zhengjie.modules.constant.FileConstant;
import me.zhengjie.modules.upload.consts.SysConst; import me.zhengjie.modules.upload.consts.SysConst;
import me.zhengjie.modules.uploadnew.domain.TbUploadFileNew; import me.zhengjie.modules.uploadnew.domain.TbUploadFileNew;
import me.zhengjie.modules.uploadnew.repository.TbUploadFileNewRepository; import me.zhengjie.modules.uploadnew.repository.TbUploadFileNewRepository;
@ -28,8 +31,7 @@ import me.zhengjie.modules.uploadnew.service.TbUploadFileNewService;
import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewDto; import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewDto;
import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewQueryCriteria; import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewQueryCriteria;
import me.zhengjie.modules.uploadnew.service.mapstruct.TbUploadFileNewMapper; import me.zhengjie.modules.uploadnew.service.mapstruct.TbUploadFileNewMapper;
import me.zhengjie.modules.uploadnew.util.ExcelUtils; import me.zhengjie.modules.uploadnew.util.ToolExcelUtils;
import me.zhengjie.modules.uploadnew.util.TxtUtils;
import me.zhengjie.utils.*; import me.zhengjie.utils.*;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page; import org.springframework.data.domain.Page;
@ -41,6 +43,7 @@ import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
@ -161,49 +164,47 @@ public class TbUploadFileNewServiceImpl implements TbUploadFileNewService {
try { try {
// 把文件保存到本地路径 // 把文件保存到本地路径
file.transferTo(Paths.get(eachFilePath)); file.transferTo(Paths.get(eachFilePath));
baseStr = "";
} catch (IOException e) { } catch (IOException e) {
log.error("============== [transferTo file fail, path is {} ] ==============", eachFilePath, e); log.error("============== [transferTo file fail, path is {} ] ==============", eachFilePath, e);
} }
String name = file.getOriginalFilename(); String name = file.getOriginalFilename();
int lastIndexOf = name.lastIndexOf("."); int lastIndexOf = name.lastIndexOf(".");
String nameStr = name.substring(lastIndexOf); String nameStr = name.substring(lastIndexOf + DefaultConstant.ONE_NUMBER);
//记录文件格式 //记录文件格式
String fileFormat = ""; String fileFormat = "";
List<String> list = null;
//根据文件类型进行解析 //根据文件类型进行解析
try { // modify the analytical data type by Enzo date 2021-9-14
// FIXME: 2021/4/23 0023 switch (nameStr) {
if (nameStr.equals(".xlsx") || nameStr.equals(".xls")) { case FileConstant
.XLSX_FILE_SUB_NAME:
case FileConstant
.XLS_FILE_SUB_NAME:
fileFormat = "excel文件"; fileFormat = "excel文件";
ExcelUtils excelUtils = new ExcelUtils(); count = ToolExcelUtils.countSizeByUrl(eachFilePath);
list = excelUtils.excelParseList(file.getInputStream()); break;
case FileConstant.TXT_FILE_SUB_NAME:
} else if (nameStr.equals(".txt")) {
fileFormat = "txt文件"; fileFormat = "txt文件";
list = FileUtil.readLines(eachFilePath, "UTF-8"); count = FileUtil.readLines(eachFilePath, StandardCharsets.UTF_8).size();
} else if (nameStr.equals(".csv")) { break;
case FileConstant.CSV_FILE_SUB_NAME:
fileFormat = "csv文件"; fileFormat = "csv文件";
list = TxtUtils.csvParseList(file.getInputStream()); count = ToolExcelUtils.countSizeByUrl(eachFilePath);
} break;
} catch (Exception e) { default:
e.printStackTrace();
} }
// 统计行数 if (count <= DefaultConstant.ZERO_NUMBER) {
if (CollectionUtil.isNotEmpty(list)) { log.error("============= file parsing exception name as {} =============",name);
count += list.size(); throw new BadRequestException(ResponseCode.FILE_HANDLE_FAIL.getDesc());
} }
// 保存所有的临时存放路径 // 保存所有的临时存放路径
pathBuilder.append(eachFilePath); pathBuilder.append(eachFilePath);
pathBuilder.append(FILE_PATH_SPLIT); pathBuilder.append(FILE_PATH_SPLIT);
// 2. 更新上传记录为正在上传,解析了有多少条 // 2. 更新上传记录为正在上传,解析了有多少条
TbUploadFileNew tbUploadFileNew = new TbUploadFileNew(); TbUploadFileNew tbUploadFileNew = new TbUploadFileNew();
tbUploadFileNew.setUploadFileTaskName(taskName); tbUploadFileNew.setUploadFileTaskName(taskName);
tbUploadFileNew.setUploadTime(new Timestamp(new Date().getTime())); tbUploadFileNew.setUploadTime(new Timestamp(System.currentTimeMillis()));
String currentUsername; String currentUsername;
if (SysConst.sysDebug) { if (SysConst.sysDebug) {
tbUploadFileNew.setOperation(TEST_USER_NAME);//fixme 这边测试环境补充一下需要的操作人 tbUploadFileNew.setOperation(TEST_USER_NAME);//fixme 这边测试环境补充一下需要的操作人
@ -211,7 +212,6 @@ public class TbUploadFileNewServiceImpl implements TbUploadFileNewService {
currentUsername = SecurityUtils.getCurrentUsername(); currentUsername = SecurityUtils.getCurrentUsername();
tbUploadFileNew.setOperation(currentUsername); tbUploadFileNew.setOperation(currentUsername);
} }
/* update by enzo update file upload type date 2021-9-7*/ /* update by enzo update file upload type date 2021-9-7*/
tbUploadFileNew.setUploadType(uploadType); tbUploadFileNew.setUploadType(uploadType);
tbUploadFileNew.setFileFormat(fileFormat); tbUploadFileNew.setFileFormat(fileFormat);

@ -13,8 +13,10 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.jcraft.jsch.Session; import com.jcraft.jsch.Session;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.zhengjie.exception.BadRequestException; import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.constant.FileConstant;
import me.zhengjie.modules.upload.task.model.ResponseEncryptJsonContent; import me.zhengjie.modules.upload.task.model.ResponseEncryptJsonContent;
import me.zhengjie.modules.upload.task.model.SendEncryptJsonContent; import me.zhengjie.modules.upload.task.model.SendEncryptJsonContent;
import me.zhengjie.modules.upload.task.model.SendRemoteUpdateJsonContent; import me.zhengjie.modules.upload.task.model.SendRemoteUpdateJsonContent;
@ -22,6 +24,7 @@ import me.zhengjie.modules.uploadnew.domain.TbUploadFileNew;
import me.zhengjie.modules.uploadnew.service.TbUploadFileNewService; import me.zhengjie.modules.uploadnew.service.TbUploadFileNewService;
import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewDto; import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewDto;
import me.zhengjie.modules.uploadnew.util.ExcelUtils; import me.zhengjie.modules.uploadnew.util.ExcelUtils;
import me.zhengjie.modules.uploadnew.util.ToolExcelUtils;
import me.zhengjie.modules.uploadnew.util.TxtUtils; import me.zhengjie.modules.uploadnew.util.TxtUtils;
import me.zhengjie.utils.ConvertUtil; import me.zhengjie.utils.ConvertUtil;
import me.zhengjie.utils.DateUtil; import me.zhengjie.utils.DateUtil;
@ -105,6 +108,7 @@ public class SaveToFileNewTask {
Long endMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli(); Long endMilliSecond = LocalDateTime.now().toInstant(ZoneOffset.of("+8")).toEpochMilli();
log.info("====== [ task start end, task name is {},cost milliSecond is {} ] ======", "SendBigDataTask", ConvertUtil.secondToTime(endMilliSecond - satrtMilliSecond)); log.info("====== [ task start end, task name is {},cost milliSecond is {} ] ======", "SendBigDataTask", ConvertUtil.secondToTime(endMilliSecond - satrtMilliSecond));
} }
/** /**
* *
* *
@ -140,22 +144,13 @@ public class SaveToFileNewTask {
* *
* @param filePath * @param filePath
*/ */
@SneakyThrows
private boolean handleEachFileContent(String filePath, TbUploadFileNewDto tbUploadFileNewDto) { private boolean handleEachFileContent(String filePath, TbUploadFileNewDto tbUploadFileNewDto) {
//根据文件类型进行解析 //根据文件类型进行解析
List<String> listT = null; List<String> listT = tbUploadFileNewDto.getFileFormat().contains(FileConstant.TXT_FILE_SUB_NAME) ?
try { TxtUtils.txtParseListVyUrl(filePath) :
if("excel文件".equals(tbUploadFileNewDto.getFileFormat())){ ToolExcelUtils.excelParseListByUrl(filePath);
ExcelUtils excelUtils = new ExcelUtils();
listT = excelUtils.excelParseListByUrl(filePath);
}else if ("txt文件".equals(tbUploadFileNewDto.getFileFormat())){
listT = TxtUtils.txtParseListVyUrl(filePath);
}else if ("csv文件".equals(tbUploadFileNewDto.getFileFormat())){
listT = TxtUtils.csvParseListByUrl(filePath);
}
}catch (Exception e){
log.error("SaveToFileTaskNew|batchSendToEncrypt ready send json is : {}", "");
throw new BadRequestException("解析文件异常");
}
Map<Integer, List<String>> preEncryptNumMap = listT.stream() Map<Integer, List<String>> preEncryptNumMap = listT.stream()
.collect(Collectors.groupingBy(String::length)); .collect(Collectors.groupingBy(String::length));
if (CollectionUtil.isNotEmpty(preEncryptNumMap)) { if (CollectionUtil.isNotEmpty(preEncryptNumMap)) {
@ -288,6 +283,7 @@ public class SaveToFileNewTask {
} }
return Boolean.FALSE; return Boolean.FALSE;
} }
private void writeToFile(List<String> collect, String fullPath) { private void writeToFile(List<String> collect, String fullPath) {
// 构建存储文件 // 构建存储文件
try { try {

@ -5,10 +5,12 @@ import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil; import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import me.zhengjie.constant.SmsConstant; import me.zhengjie.constant.SmsConstant;
import me.zhengjie.exception.BadRequestException; import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.constant.DefaultConstant; import me.zhengjie.modules.constant.DefaultConstant;
import me.zhengjie.modules.constant.FileConstant;
import me.zhengjie.modules.sms.domain.TbSendSms; import me.zhengjie.modules.sms.domain.TbSendSms;
import me.zhengjie.modules.sms.dto.ShortLinkUrlDto; import me.zhengjie.modules.sms.dto.ShortLinkUrlDto;
import me.zhengjie.modules.sms.service.TbSendSmsService; import me.zhengjie.modules.sms.service.TbSendSmsService;
@ -18,6 +20,7 @@ import me.zhengjie.modules.uploadnew.domain.TbUploadFileNew;
import me.zhengjie.modules.uploadnew.service.TbUploadFileNewService; import me.zhengjie.modules.uploadnew.service.TbUploadFileNewService;
import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewDto; import me.zhengjie.modules.uploadnew.service.dto.TbUploadFileNewDto;
import me.zhengjie.modules.uploadnew.util.ExcelUtils; import me.zhengjie.modules.uploadnew.util.ExcelUtils;
import me.zhengjie.modules.uploadnew.util.ToolExcelUtils;
import me.zhengjie.modules.uploadnew.util.TxtUtils; import me.zhengjie.modules.uploadnew.util.TxtUtils;
import me.zhengjie.utils.ConvertUtil; import me.zhengjie.utils.ConvertUtil;
import me.zhengjie.utils.FileUtil; import me.zhengjie.utils.FileUtil;
@ -28,6 +31,7 @@ import org.springframework.beans.BeanUtils;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.sql.Timestamp; import java.sql.Timestamp;
import java.time.Instant; import java.time.Instant;
@ -86,22 +90,13 @@ public class SendMessageTask {
* @param filePath * @param filePath
* @param smsConfigurationDto * @param smsConfigurationDto
*/ */
@SneakyThrows
private boolean handleEachFileContent(String filePath, String fileFormat, SmsConfigurationDto smsConfigurationDto) { private boolean handleEachFileContent(String filePath, String fileFormat, SmsConfigurationDto smsConfigurationDto) {
//根据文件类型进行解析 //根据文件类型进行解析
List<String> list = null; List<String> list = fileFormat.contains(FileConstant.TXT_FILE_SUB_NAME) ?
try { TxtUtils.txtParseListVyUrl(filePath) :
if ("excel文件".equals(fileFormat)) { ToolExcelUtils.excelParseListByUrl(filePath);
ExcelUtils excelUtils = new ExcelUtils();
list = excelUtils.excelParseListByUrl(filePath);
} else if ("txt文件".equals(fileFormat)) {
list = TxtUtils.txtParseListVyUrl(filePath);
} else if ("csv文件".equals(fileFormat)) {
list = TxtUtils.csvParseListByUrl(filePath);
}
} catch (Exception e) {
log.error("SaveToFileTaskNew|batchSendToEncrypt ready send json is : {}", "");
throw new BadRequestException("解析文件异常");
}
if (!CollectionUtils.isEmpty(list)) { if (!CollectionUtils.isEmpty(list)) {
List<String> stringList = list.stream().filter List<String> stringList = list.stream().filter
(phone -> phone.trim().getBytes(StandardCharsets.UTF_8).length == DefaultConstant.ELEVEN_NUMBER). (phone -> phone.trim().getBytes(StandardCharsets.UTF_8).length == DefaultConstant.ELEVEN_NUMBER).

@ -1,6 +1,7 @@
package me.zhengjie.modules.uploadnew.util; package me.zhengjie.modules.uploadnew.util;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
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;
@ -60,8 +61,7 @@ public class ExcelUtils {
*/ */
public List<String> excelParseListByUrl(String url) throws IOException { public List<String> excelParseListByUrl(String url) throws IOException {
List<String> list = new ArrayList<>(); List<String> list = new ArrayList<>();
InputStream inputStream = new FileInputStream(new File(url)) ; InputStream inputStream = new FileInputStream(url) ;
try {
Workbook workbook = new XSSFWorkbook(inputStream); Workbook workbook = new XSSFWorkbook(inputStream);
//获取第一张工作表 //获取第一张工作表
@ -79,13 +79,7 @@ public class ExcelUtils {
//关闭资源 //关闭资源
workbook.close(); workbook.close();
} }
} catch (FileNotFoundException e) {
log.info("ExcelUtils | excelParseList ==========未找到文件");
e.printStackTrace();
} catch (IOException e) {
log.info("ExcelUtils | excelParseList ==========解析失败!!");
e.printStackTrace();
}
return list; return list;
} }

@ -0,0 +1,45 @@
package me.zhengjie.modules.uploadnew.util;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.modules.constant.DefaultConstant;
import org.apache.commons.collections4.CollectionUtils;
import java.util.ArrayList;
import java.util.List;
/**
* @author Enzo
* @date : 2021/9/14
*/
@Slf4j
public class ToolExcelUtils {
private ToolExcelUtils() {
}
public static List<String> excelParseListByUrl(String fileUrl) {
List<String> list = new ArrayList<>();
ExcelReader reader = ExcelUtil.getReader(cn.hutool.core.io.FileUtil.file(fileUrl));
if (CollectionUtils.isNotEmpty(reader.read())) {
for (List<Object> objects : reader.read()) {
String phone = objects.toString();
if (phone.startsWith(StrUtil.BRACKET_START) && phone.endsWith(StrUtil.BRACKET_END)) {
list.add(phone.substring(DefaultConstant.ONE_NUMBER, DefaultConstant.TWELVE_NUMBER));
}
}
}
return list;
}
public static int countSizeByUrl(String fileUrl) {
ExcelReader reader = ExcelUtil.getReader(cn.hutool.core.io.FileUtil.file(fileUrl));
if (CollectionUtils.isNotEmpty(reader.read())) {
return reader.read().size();
}
return DefaultConstant.ZERO_NUMBER;
}
}

@ -6,7 +6,7 @@ spring:
freemarker: freemarker:
check-template-location: false check-template-location: false
profiles: profiles:
active: prod active: dev
jackson: jackson:
time-zone: GMT+8 time-zone: GMT+8
data: data:

Loading…
Cancel
Save