修改解析文件数据

master
bynt 3 years ago
parent 9a9f4158fe
commit 3d08d302d9

@ -40,9 +40,13 @@ public class DefaultConstant {
public static final int TEN_NUMBER = 10;
/**
* 10
* 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 {
@ -16,6 +17,21 @@ public class FileConstant {
*/
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

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

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

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

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

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

@ -13,8 +13,10 @@ import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.google.common.collect.Lists;
import com.jcraft.jsch.Session;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
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.SendEncryptJsonContent;
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.dto.TbUploadFileNewDto;
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.ConvertUtil;
import me.zhengjie.utils.DateUtil;
@ -105,6 +108,7 @@ public class SaveToFileNewTask {
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));
}
/**
*
*
@ -140,39 +144,30 @@ public class SaveToFileNewTask {
*
* @param filePath
*/
@SneakyThrows
private boolean handleEachFileContent(String filePath, TbUploadFileNewDto tbUploadFileNewDto) {
//根据文件类型进行解析
List<String> listT = null;
try {
if("excel文件".equals(tbUploadFileNewDto.getFileFormat())){
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("解析文件异常");
}
List<String> listT = tbUploadFileNewDto.getFileFormat().contains(FileConstant.TXT_FILE_SUB_NAME) ?
TxtUtils.txtParseListVyUrl(filePath) :
ToolExcelUtils.excelParseListByUrl(filePath);
Map<Integer, List<String>> preEncryptNumMap = listT.stream()
.collect(Collectors.groupingBy(String::length));
if (CollectionUtil.isNotEmpty(preEncryptNumMap)) {
// 分批调用接口进行加密
List<String> list = preEncryptNumMap.get(PRE_SEND_NUM_LENGTH);
if (CollectionUtil.isNotEmpty(list)){
if (CollectionUtil.isNotEmpty(list)) {
batchSendToEncrypt(filePath, list);
}
}
// modify by q 把剩下不需要加密的内容也写到文件中
List<String> list = preEncryptNumMap.get(SEND_NUM_LENGTH);
if (CollectionUtil.isNotEmpty(list)){
if (CollectionUtil.isNotEmpty(list)) {
writeToFile(list, filePath + TEMP_FILE_END_STR);
}
// 加入一个全局控制开关
if (!booleanTag){
if (!booleanTag) {
return Boolean.FALSE;
}
@ -183,7 +178,7 @@ public class SaveToFileNewTask {
transFileToOtherServer(filePath + TEMP_FILE_END_STR);
// 调用远程接口完成一条记录更新
boolean sendUpdatePostReqTag = sendUpdatePostReq(filePath+ TEMP_FILE_END_STR, tbUploadFileNewDto);
boolean sendUpdatePostReqTag = sendUpdatePostReq(filePath + TEMP_FILE_END_STR, tbUploadFileNewDto);
// fixme 这里要修改之前的平台给一个更新接口,然后这边可以用rpc调用,也可以用http,也可以考虑直接消息中间件进行解耦
if (delFileTag && sendUpdatePostReqTag) {
return Boolean.TRUE;
@ -288,6 +283,7 @@ public class SaveToFileNewTask {
}
return Boolean.FALSE;
}
private void writeToFile(List<String> collect, String fullPath) {
// 构建存储文件
try {
@ -330,7 +326,7 @@ public class SaveToFileNewTask {
* @param path
* @return
*/
private boolean sendUpdatePostReq(String path,TbUploadFileNewDto tbUploadFileNewDto) {
private boolean sendUpdatePostReq(String path, TbUploadFileNewDto tbUploadFileNewDto) {
// 构建发送参数
SendRemoteUpdateJsonContent sendRemoteUpdateJsonContent = new SendRemoteUpdateJsonContent();

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

@ -1,6 +1,7 @@
package me.zhengjie.modules.uploadnew.util;
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.Row;
import org.apache.poi.ss.usermodel.Sheet;
@ -60,8 +61,7 @@ public class ExcelUtils {
*/
public List<String> excelParseListByUrl(String url) throws IOException {
List<String> list = new ArrayList<>();
InputStream inputStream = new FileInputStream(new File(url)) ;
try {
InputStream inputStream = new FileInputStream(url) ;
Workbook workbook = new XSSFWorkbook(inputStream);
//获取第一张工作表
@ -79,13 +79,7 @@ public class ExcelUtils {
//关闭资源
workbook.close();
}
} catch (FileNotFoundException e) {
log.info("ExcelUtils | excelParseList ==========未找到文件");
e.printStackTrace();
} catch (IOException e) {
log.info("ExcelUtils | excelParseList ==========解析失败!!");
e.printStackTrace();
}
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:
check-template-location: false
profiles:
active: prod
active: dev
jackson:
time-zone: GMT+8
data:

Loading…
Cancel
Save