Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
A
amos-boot-biz
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
项目统一框架
amos-boot-biz
Commits
d420197d
Commit
d420197d
authored
Feb 28, 2024
by
刘凡
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
优化:【空工大】excel的文件流的解析方法
parent
cf16af0b
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
75 additions
and
34 deletions
+75
-34
ClientHandler.java
.../main/java/com/yeejoin/amos/kgd/config/ClientHandler.java
+75
-34
No files found.
amos-boot-module/amos-boot-module-api/amos-boot-module-kgd-api/src/main/java/com/yeejoin/amos/kgd/config/ClientHandler.java
View file @
d420197d
...
...
@@ -3,7 +3,6 @@ package com.yeejoin.amos.kgd.config;
import
com.alibaba.fastjson.JSONArray
;
import
com.alibaba.fastjson.JSONObject
;
import
com.github.pagehelper.util.StringUtil
;
import
com.netflix.discovery.shared.transport.decorator.EurekaHttpClientDecorator
;
import
com.yeejoin.amos.component.robot.AmosRequestContext
;
import
com.yeejoin.amos.kgd.message.Constant
;
import
com.yeejoin.amos.kgd.message.model.TableFieldModel
;
...
...
@@ -24,13 +23,11 @@ import org.typroject.tyboot.core.foundation.context.SpringContextHelper;
import
java.io.*
;
import
java.net.Socket
;
import
java.nio.charset.StandardCharsets
;
import
java.nio.file.FileSystems
;
import
java.sql.Connection
;
import
java.sql.DriverManager
;
import
java.sql.ResultSet
;
import
java.sql.Statement
;
import
java.text.SimpleDateFormat
;
import
java.util.*
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
...
...
@@ -87,7 +84,6 @@ public class ClientHandler<path> implements Runnable {
// 获取输入流和输出流
InputStream
ips
=
socket
.
getInputStream
();
ByteArrayOutputStream
outputStream2
=
new
ByteArrayOutputStream
();
byte
[]
buffer2
=
new
byte
[
20
];
int
len2
;
...
...
@@ -127,12 +123,12 @@ public class ClientHandler<path> implements Runnable {
// }
// System.out.println(outputStream2.toString());
i
ps
=
fileInputStreamFilter
(
ips
);
InputStream
contentI
ps
=
fileInputStreamFilter
(
ips
);
// 获取Excel表头内容
List
<
String
>
tableColumns
=
ExcelTool
.
readColumnsFromExcel
(
i
ps
,
filenameWithSuffix
);
List
<
String
>
tableColumns
=
ExcelTool
.
readColumnsFromExcel
(
contentI
ps
,
filenameWithSuffix
);
// 获取Excel表数据内容
List
<
List
<
String
>>
tableDatas
=
ExcelTool
.
readDataFromExcel
(
i
ps
,
filenameWithSuffix
);
List
<
List
<
String
>>
tableDatas
=
ExcelTool
.
readDataFromExcel
(
contentI
ps
,
filenameWithSuffix
);
// 新增mysql表结构和数据和数据源
if
(
StringUtil
.
isNotEmpty
(
tableName
)
&&
tableColumns
!=
null
&&
tableDatas
!=
null
){
...
...
@@ -553,13 +549,21 @@ public class ClientHandler<path> implements Runnable {
requestInfoToSocketServer
();
}
public
static
void
findDelimiter
(
InputStream
is
,
String
delimiter
)
throws
IOException
{
StringBuilder
sb
=
new
StringBuilder
();
int
nextByte
;
while
((
nextByte
=
is
.
read
())
!=
-
1
)
{
char
c
=
(
char
)
nextByte
;
sb
.
append
(
c
);
if
(
sb
.
toString
().
contains
(
delimiter
))
{
public
void
findDelimiter
(
InputStream
is
,
String
delimiter
)
throws
IOException
{
// StringBuilder sb = new StringBuilder();
// int nextByte;
// while ((nextByte = is.read()) != -1) {
// char c = (char) nextByte;
// sb.append(c);
// if (sb.toString().contains(delimiter)) {
// return;
// }
// }
// 将输入流包装成 BufferedReader
BufferedReader
bufferedReader
=
new
BufferedReader
(
new
InputStreamReader
(
is
));
String
line
;
while
((
line
=
bufferedReader
.
readLine
())
!=
null
)
{
if
(
StringUtils
.
isNotEmpty
(
line
)
&&
line
.
contains
(
delimiter
))
{
return
;
}
}
...
...
@@ -584,38 +588,75 @@ public class ClientHandler<path> implements Runnable {
return
buf
;
}
public
static
String
filenameFilter
(
InputStream
is
)
throws
IOException
{
private
InputStream
getDelimiterContent
(
InputStream
is
,
String
start
,
String
end
)
throws
IOException
{
BufferedReader
reader
=
new
BufferedReader
(
new
InputStreamReader
(
is
));
StringBuilder
contentBuilder
=
new
StringBuilder
();
boolean
isInsideTitleSection
=
false
;
String
line
;
while
((
line
=
reader
.
readLine
())
!=
null
)
{
if
(!
isInsideTitleSection
&&
line
.
contains
(
start
))
{
isInsideTitleSection
=
true
;
}
else
if
(
isInsideTitleSection
&&
line
.
contains
(
end
))
{
break
;
}
else
if
(
isInsideTitleSection
)
{
contentBuilder
.
append
(
line
).
append
(
"\n"
);
}
}
return
new
ByteArrayInputStream
(
contentBuilder
.
toString
().
getBytes
(
"UTF-8"
));
}
public
String
filenameFilter
(
InputStream
is
)
throws
IOException
{
BufferedInputStream
bis
=
new
BufferedInputStream
(
is
);
findDelimiter
(
bis
,
"##STATITLE##"
);
String
start
=
"##STATITLE##"
;
String
end
=
"##ENDTITLE##"
;
byte
[]
endDelimiter
=
"##ENDTITLE##"
.
getBytes
();
//校验内容格式
findDelimiter
(
bis
,
start
);
int
byteRead
;
byte
[]
buf
=
new
byte
[
4096
];
if
((
byteRead
=
bis
.
read
(
buf
))
!=
-
1
)
{
byte
[]
newByte
=
checkDelimiter
(
buf
,
byteRead
,
endDelimiter
);
return
new
String
(
newByte
,
"GBK"
);
//获取指定标记中的内容
InputStream
resultContent
=
getDelimiterContent
(
is
,
start
,
end
);
byte
[]
bytes
=
new
byte
[
resultContent
.
available
()];
int
byteRead
=
resultContent
.
read
(
bytes
);
if
(
byteRead
!=
-
1
){
return
new
String
(
bytes
);
}
// byte[] endDelimiter = end.getBytes();
// int byteRead;
// byte[] buf = new byte[4096];
// if ((byteRead = bis.read(buf)) != -1) {
// byte[] newByte = checkDelimiter(buf, byteRead, endDelimiter);
// return new String(newByte, "GBK");
// }
throw
new
IOException
(
"没有输入流内容或者文件名太长"
);
}
public
static
InputStream
fileInputStreamFilter
(
InputStream
is
)
throws
IOException
{
public
InputStream
fileInputStreamFilter
(
InputStream
is
)
throws
IOException
{
BufferedInputStream
bis
=
new
BufferedInputStream
(
is
);
String
fileTmpPath
=
"D:\\tmp.xlsx"
;
FileOutputStream
fos
=
new
FileOutputStream
(
fileTmpPath
);
//
String fileTmpPath = "D:\\tmp.xlsx";
//
FileOutputStream fos = new FileOutputStream(fileTmpPath);
byte
[]
endDelimiter
=
"##ENDFILE##"
.
getBytes
();
String
start
=
"##STATITLE##"
;
String
end
=
"##ENDTITLE##"
;
findDelimiter
(
bis
,
"##STAFILE##"
);
//校验内容格式
findDelimiter
(
bis
,
start
);
int
byteRead
;
byte
[]
buf
=
new
byte
[
4000
];
while
((
byteRead
=
bis
.
read
(
buf
))
!=
-
1
)
{
byte
[]
newByte
=
checkDelimiter
(
buf
,
byteRead
,
endDelimiter
);
fos
.
write
(
newByte
,
0
,
Math
.
min
(
byteRead
,
newByte
.
length
));
}
return
new
FileInputStream
(
fileTmpPath
);
//获取指定标记中的内容
InputStream
resultContent
=
getDelimiterContent
(
is
,
start
,
end
);
return
resultContent
;
// byte[] endDelimiter = end.getBytes();
// int byteRead;
// byte[] buf = new byte[4000];
// while ((byteRead = bis.read(buf)) != -1) {
// byte[] newByte = checkDelimiter(buf, byteRead, endDelimiter);
// fos.write(newByte, 0, Math.min(byteRead, newByte.length));
//
// }
// return new FileInputStream(fileTmpPath);
}
//截取文件流
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment