@ -38,7 +38,7 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
@Override
@Transactional ( rollbackFor = Exception . class )
public void saveByExcel ( ) {
public void saveByExcel ( ) {
/ / 获取每一个文件夹路径
List < EquipmentFolderLocation > locationList = equipmentFolderLocationService . list ( ) ;
if ( CollectionUtils . isEmpty ( locationList ) ) {
@ -52,7 +52,7 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
try {
/ / 对文件夹下的文件进行读取并保存
saveInformation ( excel ) ;
} catch ( Exception e ) {
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
@ -62,7 +62,6 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
executorService . shutdown ( ) ;
try {
executorService . awaitTermination ( Long . MAX_VALUE , TimeUnit . NANOSECONDS ) ; / / 等待所有任务完成
} catch ( InterruptedException e ) {
Thread . currentThread ( ) . interrupt ( ) ;
log . info ( "线程池中断异常" ) ;
@ -70,11 +69,46 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
}
}
private void saveInformation ( EquipmentFolderLocation excel ) throws Exception {
private void saveInformation ( EquipmentFolderLocation excel ) throws Exception {
String folderFiler = excel . getFolderPath ( ) ;
/ / 获取文件夹下所有文件
List < File > files = getExcelFiles ( folderFiler ) ;
if ( CollectionUtils . isEmpty ( files ) ) {
/ / List < File > files = getExcelFiles ( folderFiler ) ;
/ / 创建本地文件夹路径
File fileInfo = new File ( folderFiler ) ;
/ / 获取本地文件夹下所有文件
File [ ] files = fileInfo . listFiles ( ) ;
/ / 文件
List < File > excelFiles = new ArrayList < > ( ) ;
/ / 图片
List < String > imageFiles = new ArrayList < > ( ) ;
List < String > deleteFile = new ArrayList < > ( ) ;
if ( fileInfo ! = null & & files ! = null ) {
for ( File file1 : files ) {
try {
String name = file1 . getName ( ) ;
String timestamp = String . valueOf ( System . currentTimeMillis ( ) ) ;
/ / 构建目标文件名 , 使用源文件名加上时间戳后缀
String destinationFileName = timestamp + "$" + name ;
File renamedFile = new File ( file1 . getParent ( ) , destinationFileName ) ;
String contentType = Files . probeContentType ( file1 . toPath ( ) ) ;
if ( contentType ! = null & & contentType . startsWith ( "image" ) ) {
/ / 图片类型文件
/ / 处理图片文件的逻辑
deleteFile . add ( folderFiler + "\\" + file1 . getName ( ) ) ;
imageFiles . add ( folderFiler + "\\" + renamedFile . getName ( ) ) ;
} else {
deleteFile . add ( folderFiler + "\\" + file1 . getName ( ) ) ;
excelFiles . add ( renamedFile ) ;
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
}
if ( CollectionUtils . isEmpty ( excelFiles ) & & CollectionUtils . isEmpty ( imageFiles ) ) {
return ;
}
/ / 判断这个批次号应该是多少
@ -90,73 +124,71 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
} else {
batchNo = equipments . get ( equipments . size ( ) - 1 ) . getBatchNo ( ) + 1 ;
}
/ / 对图片保存
if ( CollectionUtils . isNotEmpty ( imageFiles ) ) {
List < EquipmentDataDetail > equipmentDataDetails = saveImage ( imageFiles , excel , batchNo ) ;
getInformationForExcelMapper . saveByExcels ( equipmentDataDetails ) ;
for ( String s : deleteFile ) {
deleteBypath ( s , excel . getBackupPhotoPath ( ) ) ;
}
}
/ / 创建一个list用来存储所有的的excel文件路径
/ / 创建一个list用来存储所有的的excel ( csv ) 文件路径
List < String > fileName = new ArrayList < > ( ) ;
/ / 创建一个list用来存储所有的tff ( txt ) 文件路径
List < String > tffName = new ArrayList < > ( ) ;
/ / 创建一个list用来存储所有的txt文件路径
List < String > txtName = new ArrayList < > ( ) ;
for ( File file : files ) {
for ( File file : excelF iles) {
/ / 找到 . 的索引
int dotIndex = file . getName ( ) . lastIndexOf ( "." ) ;
String s = file . getName ( ) . substring ( dotIndex + 1 ) ;
if ( s . equals ( "csv" ) ) {
if ( s . equals ( "csv" ) | | s . equals ( "xls" ) ) {
fileName . add ( folderFiler + "\\" + file . getName ( ) ) ;
}
if ( s . equals ( "tff" ) ) {
tffName . add ( folderFiler + "\\" + file . getName ( ) ) ;
}
if ( s . equals ( "txt" ) ) {
txtName . add ( folderFiler + "\\" + file . getName ( ) ) ;
}
}
/ / excel ( csv ) 不为空就做新增
if ( CollectionUtils . isNotEmpty ( fileName ) ) {
List < EquipmentDataDetail > equipmentDataDetails = saveExcel ( fileName , excel , batchNo ) ;
if ( CollectionUtils . isNotEmpty ( equipmentDataDetails ) ) {
getInformationForExcelMapper . saveByExcels ( equipmentDataDetails ) ;
if ( CollectionUtils . isNotEmpty ( equipmentDataDetails ) ) {
/ / 批量新增
for ( String s : fileName ) {
getInformationForExcelMapper . saveByExcels ( equipmentDataDetails ) ;
for ( String s : deleteFile ) {
deleteBypath ( s , excel . getBackupFolderPath ( ) ) ;
}
} else {
} else {
return ;
}
}
/ / txt ( tff ) 不为空就做新增
if ( CollectionUtils . isNotEmpty ( txtName ) ) {
List < EquipmentDataDetail > equipmentDataDetails = saveTxt ( txtName , excel , batchNo ) ;
/ / 如果需要新增的数量小于一百就直接新增 , 大于一百就做线程池新增
if ( equipmentDataDetails . size ( ) < 100 ) {
/ / 批量新增
getInformationForExcelMapper . saveByExcels ( equipmentDataDetails ) ;
} else {
/ / 创建一个固定大小的线程池 , 这里假设有10个线程
ExecutorService executor = Executors . newFixedThreadPool ( equipmentDataDetails . size ( ) / 100 ) ;
int startList = 0 ;
int endList = 100 ;
for ( int i = startList ; i < equipmentDataDetails . size ( ) ; i = startList ) {
if ( endList > equipmentDataDetails . size ( ) ) {
endList = equipmentDataDetails . size ( ) ;
}
List < EquipmentDataDetail > details = equipmentDataDetails . subList ( startList , endList ) ;
startList = startList + endList ;
endList = endList + endList ;
executor . execute ( ( ) - > {
/ / 批量新增
getInformationForExcelMapper . saveByExcels ( details ) ;
} ) ;
}
/ / 关闭线程池
executor . shutdown ( ) ;
if ( CollectionUtils . isNotEmpty ( tffName ) ) {
List < EquipmentDataDetail > equipmentDataDetails = saveTxt ( tffName , excel , batchNo ) ;
/ / 新增数据
saveInformation ( equipmentDataDetails ) ;
for ( String s : deleteFile ) {
deleteBypath ( s , excel . getBackupFolderPath ( ) ) ;
}
for ( String s : txtName ) {
deleteBypath ( s , excel . getBackupFolderPath ( ) ) ;
}
/ / txt文件不为空就做新增
if ( CollectionUtils . isNotEmpty ( txtName ) ) {
List < EquipmentDataDetail > equipmentDataDetails1 = saveTxtFile ( txtName , excel , batchNo ) ;
saveInformation ( equipmentDataDetails1 ) ;
for ( String s : deleteFile ) {
deleteBypath ( s , excel . getBackupFolderPath ( ) ) ;
}
}
}
/ / 新增完之后把原路径的文件复制到另一个文件并删除原路径的下的文件
private static void deleteBypath ( String endPath , String suffixName ) throws Exception {
private static void deleteBypath ( String endPath , String suffixName ) throws Exception {
/ / 创建源文件对象和目标文件夹对象
File sourceFile = new File ( endPath ) ;
File destinationDir = new File ( suffixName ) ;
@ -168,20 +200,23 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
destinationDir . mkdirs ( ) ; / / 创建目标文件夹及其父目录
}
String timestamp = String . valueOf ( System . currentTimeMillis ( ) ) ;
/ / 构建目标文件名 , 使用源文件名加上时间戳后缀
String destinationFileName = timestamp + "$" + sourceFile . getName ( ) ;
/ / 构建目标文件路径
String destinationPath = suffixName + File . separator + sourceFile . getName ( ) ;
String destinationPath = suffixName + File . separator + destinationFileName ;
File destinationFile = new File ( destinationPath ) ;
try {
/ / 复制文件到目标文件夹
Files . copy ( sourceFile . toPath ( ) , destinationFile . toPath ( ) ) ;
log . info ( "文件复制成功!" ) ;
log . info ( "文件复制成功!" + sourceFile . toPath ( ) ) ;
/ / 删除源文件
if ( sourceFile . delete ( ) ) {
log . info ( "源文件删除成功!" ) ;
log . info ( "源文件删除成功!" + sourceFile . toPath ( ) ) ;
} else {
log . info ( "源文件删除失败!" ) ;
log . info ( "源文件删除失败!" + sourceFile . toPath ( ) ) ;
}
} catch ( IOException e ) {
log . info ( "文件操作出错:" + e . getMessage ( ) ) ;
@ -228,7 +263,7 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
line = bufferedReader . readLine ( ) ;
}
/ / 移动到下一列
for ( int i = 0 ; i < columnInterval - 1 ; i + + ) {
for ( int i = 0 ; i < columnInterval - 1 ; i + + ) {
line = bufferedReader . readLine ( ) ;
}
}
@ -242,7 +277,6 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
equipmentDataDetail . setBatchNo ( batchNo ) ;
equipmentDataDetail . setItemNo ( excel . getItemNo ( ) ) ;
equipmentDataDetail . setFileNo ( excel . getFileNo ( ) ) ;
equipmentDataDetails . add ( equipmentDataDetail ) ;
equipmentDataDetail . setValue0 ( data [ 0 ] ) ;
equipmentDataDetail . setValue1 ( data [ 1 ] ) ;
equipmentDataDetail . setValue2 ( data [ 2 ] ) ;
@ -280,13 +314,13 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
e . printStackTrace ( ) ;
log . info ( "读取文件失败,文件为:" + s + "失败日志:" + e . getMessage ( ) ) ;
}
if ( CollectionUtils . isEmpty ( list ) ) {
if ( CollectionUtils . isEmpty ( list ) ) {
log . info ( "读取的文件为空,或不是excel格式的" ) ;
return new ArrayList < > ( ) ;
}
/ / 获取TEST后面的两个值
for ( int i = 0 ; i < list . size ( ) ; i + + ) {
if ( list . get ( i ) . get ( 0 ) = = null ) {
if ( list . get ( i ) . get ( 0 ) = = null ) {
continue ;
}
if ( list . get ( i ) . get ( 0 ) . equals ( "TEST" ) ) {
@ -305,20 +339,145 @@ public class GetInformationForExcelServiceImpl extends ServiceImpl<GetInformatio
}
return equipmentDataDetails ;
}
/ / 获取文件夹下的所有文件
private static List < File > getExcelFiles ( String folderFiler ) {
/ / 对txt文件进行操作
private static List < EquipmentDataDetail > saveTxtFile ( List < String > fileName , EquipmentFolderLocation excel , Integer batchNo ) {
List < EquipmentDataDetail > detailList = new ArrayList < > ( ) ;
for ( String name : fileName ) {
try {
BufferedReader bufferedReader = new BufferedReader ( new FileReader ( name ) ) ;
String line ;
List < String > list = new ArrayList < > ( ) ;
for ( int i = 1 ; i < = 19 ; i + + ) {
bufferedReader . readLine ( ) ; / / 跳过前面的行数
}
while ( ( line = bufferedReader . readLine ( ) ) ! = null ) {
if ( line ! = null ) {
/ / 处理当前行数据
list . add ( line ) ;
}
}
for ( String s : list ) {
EquipmentDataDetail equipmentDataDetail = new EquipmentDataDetail ( ) ;
String [ ] data = s . split ( "\t" ) ;
equipmentDataDetail . setEquipmentNo ( excel . getEquipmentNo ( ) ) ;
equipmentDataDetail . setBuNo ( excel . getBuNo ( ) ) ;
equipmentDataDetail . setSite ( excel . getSite ( ) ) ;
equipmentDataDetail . setBatchNo ( batchNo ) ;
equipmentDataDetail . setItemNo ( excel . getItemNo ( ) ) ;
equipmentDataDetail . setFileNo ( excel . getFileNo ( ) ) ;
equipmentDataDetail . setValue0 ( data [ 0 ] ) ;
equipmentDataDetail . setValue1 ( data [ 1 ] ) ;
equipmentDataDetail . setValue2 ( data [ 2 ] ) ;
equipmentDataDetail . setValue3 ( data [ 3 ] ) ;
detailList . add ( equipmentDataDetail ) ;
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
return detailList ;
}
/ / 对图片进行保存
private static List < EquipmentDataDetail > saveImage ( List < String > fileName , EquipmentFolderLocation excel , Integer batchNo ) {
List < EquipmentDataDetail > detailList = new ArrayList < > ( ) ;
for ( String s : fileName ) {
EquipmentDataDetail equipmentDataDetail = new EquipmentDataDetail ( ) ;
equipmentDataDetail . setEquipmentNo ( excel . getEquipmentNo ( ) ) ;
equipmentDataDetail . setBuNo ( excel . getBuNo ( ) ) ;
equipmentDataDetail . setSite ( excel . getSite ( ) ) ;
equipmentDataDetail . setBatchNo ( batchNo ) ;
equipmentDataDetail . setItemNo ( excel . getItemNo ( ) ) ;
equipmentDataDetail . setFileNo ( excel . getFileNo ( ) ) ;
equipmentDataDetail . setPhotoValue1 ( s ) ;
detailList . add ( equipmentDataDetail ) ;
}
return detailList ;
}
/ / / / 获取文件夹下的所有文件
/ / private static List < File > getExcelFiles ( String folderFiler ) {
/ / / / 创建本地文件夹路径
/ / File file = new File ( folderFiler ) ;
/ / / / 获取本地文件夹下所有文件
/ / File [ ] files = file . listFiles ( ) ;
/ / List < File > excelFiles = new ArrayList < > ( ) ;
/ / if ( file ! = null & & files ! = null ) {
/ / for ( File file1 : files ) {
/ / if ( file1 . isFile ( ) ) {
/ / excelFiles . add ( file1 ) ;
/ / }
/ / }
/ / }
/ / return excelFiles ;
/ / }
/ / 超过一百条数据做线程池新增新增
private void saveInformation ( List < EquipmentDataDetail > equipmentDataDetails ) {
/ / 如果需要新增的数量小于一百就直接新增 , 大于一百就做线程池新增
if ( equipmentDataDetails . size ( ) < 100 ) {
/ / 批量新增
getInformationForExcelMapper . saveByExcels ( equipmentDataDetails ) ;
} else {
/ / 创建一个固定大小的线程池 , 这里假设有10个线程
ExecutorService executor = Executors . newFixedThreadPool ( equipmentDataDetails . size ( ) / 100 ) ;
int startList = 0 ;
int endList = 100 ;
for ( int i = startList ; i < equipmentDataDetails . size ( ) ; i = startList ) {
if ( endList > equipmentDataDetails . size ( ) ) {
endList = equipmentDataDetails . size ( ) ;
}
List < EquipmentDataDetail > details = equipmentDataDetails . subList ( startList , endList ) ;
startList = startList + endList ;
endList = endList + endList ;
executor . execute ( ( ) - > {
/ / 批量新增
getInformationForExcelMapper . saveByExcels ( details ) ;
} ) ;
}
/ / 关闭线程池
executor . shutdown ( ) ;
}
}
public static void main ( String [ ] args ) {
String folderFiler = "E:\\file" ;
/ / 创建本地文件夹路径
File file = new File ( folderFiler ) ;
/ / 获取本地文件夹下所有文件
File [ ] files = file . listFiles ( ) ;
List < File > excelFiles = new ArrayList < > ( ) ;
if ( file ! = null & & files ! = null ) {
List < File > imageFiles = new ArrayList < > ( ) ;
List < String > s1 = new ArrayList < > ( ) ;
List < String > s2 = new ArrayList < > ( ) ;
if ( file ! = null & & files ! = null ) {
for ( File file1 : files ) {
if ( file1 . isFile ( ) ) {
excelFiles . add ( file1 ) ;
try {
String contentType = Files . probeContentType ( file1 . toPath ( ) ) ;
if ( contentType ! = null & & contentType . startsWith ( "image" ) ) {
/ / 图片类型文件
/ / 处理图片文件的逻辑
imageFiles . add ( file1 ) ;
String s = folderFiler + "\\" + file1 . getName ( ) ;
s1 . add ( s ) ;
} else {
excelFiles . add ( file1 ) ;
String s3 = folderFiler + "\\" + file1 . getName ( ) ;
s2 . add ( s3 ) ;
}
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
}
}
return excelFiles ;
System . out . println ( s1 ) ;
System . out . println ( s2 ) ;
}
}