@ -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 ) ;