Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
G
GatewayApiTestUtil
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Service Desk
Milestones
Merge Requests
1
Merge Requests
1
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Operations
Operations
Incidents
Environments
Packages & Registries
Packages & Registries
Package Registry
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
一兵 刘
GatewayApiTestUtil
Commits
43cde115
Commit
43cde115
authored
Oct 30, 2023
by
fa_pms01
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
初版 Gateway 并发测试工具
parents
Pipeline
#474
failed with stages
in 0 seconds
Changes
7
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
1099 additions
and
0 deletions
+1099
-0
pom.xml
pom.xml
+180
-0
src/main/java/WindowStart.java
src/main/java/WindowStart.java
+197
-0
src/main/java/bean/Model.java
src/main/java/bean/Model.java
+30
-0
src/main/java/bean/Param.java
src/main/java/bean/Param.java
+34
-0
src/main/java/listener/InputListener.java
src/main/java/listener/InputListener.java
+16
-0
src/main/java/utils/HandleFileUtils.java
src/main/java/utils/HandleFileUtils.java
+519
-0
src/main/java/utils/ThirdApiCaller.java
src/main/java/utils/ThirdApiCaller.java
+123
-0
No files found.
pom.xml
0 → 100644
View file @
43cde115
<?xml version="1.0" encoding="UTF-8"?>
<project
xmlns=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>
4.0.0
</modelVersion>
<properties>
<project.encoding>
UTF-8
</project.encoding>
<java.version>
1.8
</java.version>
<maven.compiler.plugin>
3.1
</maven.compiler.plugin>
<maven.source.plugin>
3.0.0
</maven.source.plugin>
<maven.javadoc.plugin>
2.9.1
</maven.javadoc.plugin>
</properties>
<groupId>
fa.com
</groupId>
<artifactId>
test
</artifactId>
<version>
0.0.1
</version>
<build>
<plugins>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-compiler-plugin
</artifactId>
<version>
${maven.compiler.plugin}
</version>
<configuration>
<encoding>
${project.encoding}
</encoding>
<source>
${java.version}
</source>
<target>
${java.version}
</target>
</configuration>
</plugin>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-dependency-plugin
</artifactId>
<version>
3.0.1
</version>
<executions>
<execution>
<id>
copy-dependencies
</id>
<phase>
package
</phase>
<goals>
<goal>
copy-dependencies
</goal>
</goals>
<configuration>
<outputDirectory>
${project.build.directory}/lib
</outputDirectory>
<overWriteReleases>
false
</overWriteReleases>
<overWriteSnapshots>
false
</overWriteSnapshots>
<overWriteIfNewer>
true
</overWriteIfNewer>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-assembly-plugin
</artifactId>
<version>
3.0.0
</version>
<configuration>
<archive>
<manifest>
<mainClass></mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>
jar-with-dependencies
</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>
make-assembly
</id>
<!-- this is used for inheritance merges -->
<phase>
package
</phase>
<!-- 指定在打包节点执行jar包合并操作 -->
<goals>
<goal>
single
</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-maven-plugin
</artifactId>
<configuration>
<!-- 包含本地system jar-->
<includeSystemScope>
true
</includeSystemScope>
</configuration>
</plugin>
<plugin>
<groupId>
org.apache.maven.plugins
</groupId>
<artifactId>
maven-resources-plugin
</artifactId>
<configuration>
<encoding>
UTF-8
</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>
xlsx
</nonFilteredFileExtension>
<nonFilteredFileExtension>
xls
</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>
com.fa.sesa.novel
</groupId>
<artifactId>
model
</artifactId>
<version>
0.0.1-SNAPSHOT
</version>
</dependency>
<dependency>
<groupId>
com.fa.sesa
</groupId>
<artifactId>
sesa-utils
</artifactId>
<version>
0.0.30-SNAPSHOT
</version>
</dependency>
<dependency>
<groupId>
org.springframework
</groupId>
<artifactId>
spring-web
</artifactId>
<version>
5.3.22
</version>
</dependency>
<dependency>
<groupId>
org.apache.poi
</groupId>
<artifactId>
poi
</artifactId>
<version>
4.1.0
</version>
</dependency>
<dependency>
<groupId>
org.apache.poi
</groupId>
<artifactId>
poi-ooxml
</artifactId>
<version>
4.1.0
</version>
</dependency>
</dependencies>
<!-- 代码库 -->
<pluginRepositories>
<pluginRepository>
<id>
fa-central
</id>
<name>
fa-central
</name>
<url>
http://devops.fa-software.com/nexus/repository/fa-central/
</url>
<releases>
<enabled>
true
</enabled>
</releases>
<snapshots>
<enabled>
true
</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
<repositories>
<repository>
<id>
fa-mycim-release
</id>
<name>
fa-mycim-release
</name>
<url>
http://devops.fa-software.com/nexus/repository/mycim-release/
</url>
<releases>
<enabled>
true
</enabled>
</releases>
</repository>
<repository>
<id>
fa-novel-release
</id>
<name>
fa-novel-release
</name>
<url>
http://devops.fa-software.com/nexus/repository/novel-release/
</url>
<releases>
<enabled>
true
</enabled>
</releases>
</repository>
<repository>
<id>
fa-mycim-snapshot
</id>
<name>
fa-mycim-snapshot
</name>
<url>
http://devops.fa-software.com/nexus/repository/mycim-snapshot/
</url>
<snapshots>
<enabled>
true
</enabled>
<updatePolicy>
always
</updatePolicy>
</snapshots>
</repository>
<repository>
<id>
fa-novel-snapshot
</id>
<name>
fa-novel-snapshot
</name>
<url>
http://devops.fa-software.com/nexus/repository/novel-snapshot/
</url>
<snapshots>
<enabled>
true
</enabled>
<updatePolicy>
always
</updatePolicy>
</snapshots>
</repository>
</repositories>
</project>
\ No newline at end of file
src/main/java/WindowStart.java
0 → 100644
View file @
43cde115
import
bean.Model
;
import
bean.Param
;
import
com.mycim.framework.utils.lang.StringUtils
;
import
com.mycim.framework.utils.lang.collections.CollectionUtils
;
import
com.mycim.framework.utils.lang.collections.MapUtils
;
import
com.mycim.framework.utils.msg.JsonUtils
;
import
utils.HandleFileUtils
;
import
utils.ThirdApiCaller
;
import
javax.swing.*
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.*
;
import
java.util.concurrent.ForkJoinPool
;
import
java.util.concurrent.ForkJoinTask
;
/**
* @Author: yibing.liu
* @Date: 2023/10/28 21:49
*/
public
class
WindowStart
extends
JFrame
{
private
JTextField
fileTextFile
;
private
JTextField
urlTextFile
;
private
JTextArea
inTextArea
;
private
JTextArea
outTextArea
;
private
JComboBox
<
Model
>
modelBox
;
private
JComboBox
<
Model
>
coreNumBox
;
private
List
<
Param
>
paramList
=
new
ArrayList
<>();
public
static
final
String
SPACE
=
"\n\r"
;
public
static
void
main
(
String
[]
args
)
{
new
WindowStart
();
}
public
WindowStart
(){
JFrame
frame
=
new
JFrame
(
"MES2.0+接口测试小程序"
);
frame
.
setBounds
(
300
,
200
,
1100
,
800
);
//设置大小和位置
JButton
fileInput
=
new
JButton
(
"读取文件夹"
);
fileInput
.
addActionListener
(
e
->
{
String
fileRootPath
=
fileTextFile
.
getText
();
inTextArea
.
setText
(
"开始读取文件夹内容:"
+
SPACE
+
"解析文件夹地址: "
+
fileRootPath
+
" "
+
SPACE
);
//先获取文件夹下所有的文件名称
List
<
String
>
fileFullNames
=
HandleFileUtils
.
readDirectoryFileFullName
(
fileRootPath
,
"txt"
,
"xlsx"
);
String
str
=
StringUtils
.
join
(
fileFullNames
,
SPACE
);
inTextArea
.
setText
(
inTextArea
.
getText
()
+
"解析文件名:"
+
SPACE
+
str
);
outTextArea
.
setText
(
""
);
//循环 根据名称读取对应的文件
if
(
CollectionUtils
.
isNotEmpty
(
fileFullNames
))
{
StringBuilder
sb
=
new
StringBuilder
(
"开始解析文本请求内容:"
).
append
(
SPACE
);
List
<
Param
>
paramListTemp
=
buildTxtData
(
fileFullNames
,
sb
);
if
(
CollectionUtils
.
isNotEmpty
(
paramListTemp
)){
paramList
=
paramListTemp
;
inTextArea
.
setText
(
inTextArea
.
getText
()
+
SPACE
+
sb
.
toString
());
}
}
});
fileTextFile
=
new
JTextField
(
"C:\\Users\\GetWind\\Desktop\\TestCaseFileRootPath"
,
50
);
// fileTextFile.addActionListener(new InputListener());
frame
.
add
(
fileTextFile
);
frame
.
add
(
fileInput
);
urlTextFile
=
new
JTextField
(
"http://localhost:9080/gateway/httpServer"
,
50
);
// urlTextFile.addActionListener(new InputListener());
frame
.
add
(
urlTextFile
);
urlTextFile
=
new
JTextField
(
"http://localhost:9080/gateway/httpServer"
,
50
);
inTextArea
=
new
JTextArea
(
10
,
30
);
// 设置行数和列数
inTextArea
.
setLineWrap
(
true
);
// 设置自动换行
inTextArea
.
setWrapStyleWord
(
true
);
// 设置按单词换行
// inTextArea.set
JScrollPane
inScrollPane
=
new
JScrollPane
(
inTextArea
);
// 添加滚动条
outTextArea
=
new
JTextArea
(
10
,
30
);
// 设置行数和列数
outTextArea
.
setLineWrap
(
true
);
// 设置自动换行
outTextArea
.
setWrapStyleWord
(
true
);
// 设置按单词换行
JScrollPane
outScrollPane
=
new
JScrollPane
(
outTextArea
);
// 添加滚动条
JButton
startButton
=
new
JButton
(
"Start"
);
startButton
.
addActionListener
(
e
->
{
String
url
=
urlTextFile
.
getText
();
Model
model
=
(
Model
)
modelBox
.
getSelectedItem
();
Model
coreNum
=
(
Model
)
coreNumBox
.
getSelectedItem
();
outTextArea
.
setText
(
"开始执行: 运行模式[ "
+
model
.
getModel
()
+
" ],线程数[ "
+
coreNum
.
getModel
()
+
" ]"
+
SPACE
);
callTest
(
paramList
,
url
,
model
.
getModelCode
(),
coreNum
);
});
modelBox
=
new
JComboBox
<>();
modelBox
.
addItem
(
new
Model
(
"并行"
,
1
));
modelBox
.
addItem
(
new
Model
(
"串行"
,
0
));
modelBox
.
addActionListener
(
e
->
{
Model
model
=
(
Model
)
modelBox
.
getSelectedItem
();
if
(
model
.
getModelCode
()
==
0
){
frame
.
remove
(
coreNumBox
);
}
else
{
frame
.
add
(
coreNumBox
);
}
});
coreNumBox
=
new
JComboBox
<>();
coreNumBox
.
addItem
(
new
Model
(
"10"
,
10
));
coreNumBox
.
addItem
(
new
Model
(
"9"
,
9
));
coreNumBox
.
addItem
(
new
Model
(
"8"
,
8
));
coreNumBox
.
addItem
(
new
Model
(
"7"
,
7
));
coreNumBox
.
addItem
(
new
Model
(
"6"
,
6
));
coreNumBox
.
addItem
(
new
Model
(
"5"
,
5
));
coreNumBox
.
addItem
(
new
Model
(
"4"
,
4
));
coreNumBox
.
addItem
(
new
Model
(
"3"
,
3
));
coreNumBox
.
addItem
(
new
Model
(
"2"
,
2
));
coreNumBox
.
setSelectedIndex
(
5
);
frame
.
setLayout
(
null
);
//没有布置可以用这个设计
fileTextFile
.
setBounds
(
50
,
50
,
450
,
30
);
fileInput
.
setBounds
(
860
,
225
,
100
,
30
);
//设置组件大小和位置
urlTextFile
.
setBounds
(
50
,
100
,
450
,
30
);
//设置组件大小和位置
inScrollPane
.
setBounds
(
50
,
150
,
800
,
200
);
outScrollPane
.
setBounds
(
50
,
350
,
800
,
300
);
startButton
.
setBounds
(
860
,
475
,
100
,
30
);
modelBox
.
setBounds
(
970
,
475
,
100
,
30
);
coreNumBox
.
setBounds
(
970
,
510
,
100
,
30
);
frame
.
add
(
fileTextFile
);
frame
.
add
(
fileInput
);
frame
.
add
(
urlTextFile
);
frame
.
add
(
outScrollPane
);
frame
.
add
(
inScrollPane
);
frame
.
add
(
startButton
);
frame
.
add
(
modelBox
);
frame
.
add
(
coreNumBox
);
frame
.
setVisible
(
true
);
//显示
frame
.
setResizable
(
false
);
//固定大小
frame
.
setDefaultCloseOperation
(
JFrame
.
EXIT_ON_CLOSE
);
//可以关闭窗口
}
private
void
callTest
(
List
<
Param
>
paramList
,
String
url
,
int
model
,
Model
coreNum
)
{
rebuildList
(
paramList
,
url
);
StringBuffer
str
=
new
StringBuffer
();
if
(
model
==
1
){
int
coreCount
=
5
;
if
(
coreNum
!=
null
){
coreCount
=
coreNum
.
getModelCode
();
}
ForkJoinPool
pool
=
new
ForkJoinPool
(
coreCount
);
ForkJoinTask
task
=
pool
.
submit
(()
->
{
paramList
.
parallelStream
().
forEach
(
s
->
str
.
append
(
call
(
s
.
getHead
(),
s
.
getBody
(),
s
.
getUrl
())));
});
task
.
join
();
pool
.
shutdown
();
}
else
{
paramList
.
forEach
(
s
->
str
.
append
(
call
(
s
.
getHead
(),
s
.
getBody
(),
s
.
getUrl
())));
}
outTextArea
.
setText
(
outTextArea
.
getText
()
+
str
.
toString
());
}
private
void
rebuildList
(
List
<
Param
>
paramList
,
String
url
)
{
paramList
.
forEach
(
p
->
p
.
setUrl
(
url
));
}
private
static
String
call
(
Map
header
,
Map
bodyObject
,
String
url
){
StringBuilder
result
=
new
StringBuilder
();
result
.
append
(
"time=["
).
append
(
System
.
currentTimeMillis
()).
append
(
"] "
).
append
(
SPACE
);
try
{
Map
<
String
,
Object
>
argMap
=
new
HashMap
<>();
argMap
.
put
(
"head"
,
header
);
argMap
.
put
(
"body"
,
bodyObject
);
result
.
append
(
"Request:"
).
append
(
bodyObject
).
append
(
SPACE
).
append
(
"Response:"
);
result
.
append
(
ThirdApiCaller
.
postJson
(
url
,
argMap
,
header
,
String
.
class
));
}
catch
(
Exception
e
)
{
result
.
append
(
"Response Error:["
).
append
(
e
.
getMessage
()).
append
(
"]"
);
}
return
result
.
append
(
SPACE
).
append
(
SPACE
).
toString
();
}
private
List
<
Param
>
buildTxtData
(
List
<
String
>
fileFullNames
,
StringBuilder
sb
)
{
List
<
String
>
failFullNames
=
new
ArrayList
<>();
List
<
Param
>
dtoList
=
new
ArrayList
<>();
for
(
String
fileFullName
:
fileFullNames
)
{
try
{
String
txtDataStr
=
HandleFileUtils
.
readTxtFile
(
fileFullName
);
Map
txtMap
=
JsonUtils
.
toMap
(
txtDataStr
);
sb
.
append
(
txtMap
.
toString
()).
append
(
SPACE
);
Map
head
=
MapUtils
.
getMap
(
txtMap
,
"head"
);
Map
body
=
MapUtils
.
getMap
(
txtMap
,
"body"
);
dtoList
.
add
(
new
Param
(
head
,
body
,
""
));
}
catch
(
Exception
e
)
{
sb
.
append
(
"文件解析失败:"
).
append
(
e
.
getMessage
()).
append
(
SPACE
);
failFullNames
.
add
(
fileFullName
);
}
sb
.
append
(
SPACE
);
}
if
(
CollectionUtils
.
isNotEmpty
(
failFullNames
))
{
failFullNames
.
forEach
(
f
->
sb
.
append
(
"解析失败的文件名:"
).
append
(
f
).
append
(
SPACE
));
}
return
dtoList
;
}
}
src/main/java/bean/Model.java
0 → 100644
View file @
43cde115
package
bean
;
/**
* @Author: yibing.liu
* @Date: 2023/10/29 16:43
*/
public
class
Model
{
private
String
model
;
private
int
modelCode
;
public
Model
(
String
model
,
int
modelCode
)
{
this
.
model
=
model
;
this
.
modelCode
=
modelCode
;
}
public
int
getModelCode
()
{
return
modelCode
;
}
public
String
getModel
()
{
return
model
;
}
@Override
public
String
toString
()
{
return
model
;
}
}
src/main/java/bean/Param.java
0 → 100644
View file @
43cde115
package
bean
;
import
java.util.Map
;
/**
* @Author: yibing.liu
* @Date: 2023/10/29 0:09
*/
public
class
Param
{
private
Map
head
;
private
Map
body
;
private
String
url
;
public
Param
(
Map
header
,
Map
bodyObject
,
String
url
){
this
.
head
=
header
;
this
.
body
=
bodyObject
;
this
.
url
=
url
;
}
public
Map
getHead
()
{
return
head
;
}
public
Map
getBody
()
{
return
body
;
}
public
String
getUrl
()
{
return
url
;
}
public
void
setUrl
(
String
url
)
{
this
.
url
=
url
;
}
}
src/main/java/listener/InputListener.java
0 → 100644
View file @
43cde115
package
listener
;
import
java.awt.*
;
import
java.awt.event.ActionEvent
;
import
java.awt.event.ActionListener
;
/**
* @Author: yibing.liu
* @Date: 2023/10/28 23:44
*/
public
class
InputListener
implements
ActionListener
{
public
void
actionPerformed
(
ActionEvent
e
)
{
TextField
textField
=
(
TextField
)
e
.
getSource
();
System
.
out
.
println
(
textField
.
getText
());
}
}
src/main/java/utils/HandleFileUtils.java
0 → 100644
View file @
43cde115
package
utils
;
import
com.mycim.framework.utils.lang.StringUtils
;
import
com.mycim.framework.utils.lang.collections.CollectionUtils
;
import
org.apache.poi.hssf.usermodel.HSSFCell
;
import
org.apache.poi.hssf.usermodel.HSSFRow
;
import
org.apache.poi.hssf.usermodel.HSSFSheet
;
import
org.apache.poi.hssf.usermodel.HSSFWorkbook
;
import
org.apache.poi.ss.usermodel.*
;
import
org.apache.poi.xssf.usermodel.XSSFCell
;
import
org.apache.poi.xssf.usermodel.XSSFRow
;
import
org.apache.poi.xssf.usermodel.XSSFSheet
;
import
org.apache.poi.xssf.usermodel.XSSFWorkbook
;
import
java.io.*
;
import
java.nio.file.Files
;
import
java.nio.file.Paths
;
import
java.nio.file.StandardCopyOption
;
import
java.text.SimpleDateFormat
;
import
java.util.*
;
public
class
HandleFileUtils
{
private
static
final
String
XLS_TYPE
=
"XLS"
;
private
static
final
String
XLSX_TYPE
=
"XLSX"
;
private
static
final
String
C_COLON
=
":"
;
private
static
final
String
C_COMMA
=
","
;
public
static
final
String
DOT
=
"."
;
/**
* 获取目标路径下指定后缀文件,并过滤掉指定后缀
*
* @param directory 指定文件夹
* @param postFix 需要获取的文件后缀
* @param excludePostFix 需要过滤的文件后缀
* @return
*/
public
static
List
<
String
>
readDirectoryFileFullName
(
String
directory
,
String
postFix
,
String
excludePostFix
)
{
List
<
String
>
fileFullNames
=
new
ArrayList
<>();
File
file
=
new
File
(
directory
);
if
(
file
.
isDirectory
())
{
File
[]
files
=
file
.
listFiles
();
for
(
File
file1
:
files
)
{
if
(
file1
.
isDirectory
())
{
fileFullNames
.
addAll
(
readDirectoryFileFullName
(
file1
.
getPath
(),
postFix
,
excludePostFix
));
}
else
{
String
fileNme
=
StringUtils
.
upperCase
(
file1
.
getName
());
if
(
StringUtils
.
isNotEmpty
(
excludePostFix
)
&&
StringUtils
.
endsWith
(
fileNme
,
StringUtils
.
upperCase
(
excludePostFix
)))
{
continue
;
}
if
(
StringUtils
.
endsWith
(
fileNme
,
StringUtils
.
upperCase
(
postFix
)))
{
fileFullNames
.
add
(
file1
.
getPath
());
}
}
}
}
return
fileFullNames
;
}
/**
* 获取目标路径下指定后缀文件,并过滤掉指定后缀
*
* @param directory 指定文件夹
* @param postFix 需要获取的文件后缀
* @param excludePostFix 需要过滤的文件后缀
* @return
*/
public
static
Map
<
String
,
String
>
readDirectoryFileNameForMap
(
String
directory
,
List
<
String
>
postFix
,
List
<
String
>
excludePostFix
,
long
time
)
{
Map
<
String
,
String
>
fileFullNames
=
new
HashMap
<>();
File
file
=
new
File
(
directory
);
if
(
file
.
isDirectory
())
{
File
[]
files
=
file
.
listFiles
();
for
(
File
file1
:
files
)
{
if
(
file1
.
isDirectory
())
{
fileFullNames
.
putAll
(
readDirectoryFileNameForMap
(
file1
.
getPath
(),
postFix
,
excludePostFix
,
time
));
}
else
{
if
(
file1
.
lastModified
()
<
time
)
continue
;
String
fileNme
=
StringUtils
.
upperCase
(
file1
.
getName
());
boolean
excludeFlag
=
false
;
if
(
CollectionUtils
.
isNotEmpty
(
excludePostFix
))
{
for
(
String
excludeFix
:
excludePostFix
)
{
if
(
StringUtils
.
endsWith
(
fileNme
,
StringUtils
.
upperCase
(
excludeFix
)))
{
excludeFlag
=
true
;
break
;
}
}
if
(
excludeFlag
)
{
continue
;
}
}
String
filePostFix
=
StringUtils
.
substringAfterLast
(
fileNme
,
DOT
);
if
(
postFix
.
contains
(
filePostFix
))
{
fileFullNames
.
put
(
file1
.
getName
(),
file1
.
getPath
());
}
}
}
}
return
fileFullNames
;
}
/**
* 解析 csv文件
*
* @param fileFullPath
* @return
*/
public
static
Map
<
String
,
Object
>
readCsvFile
(
String
fileFullPath
)
{
BufferedReader
br
=
null
;
String
line
=
null
;
StringBuffer
buf
=
new
StringBuffer
();
String
code
=
judgeTextCode
(
fileFullPath
);
Map
<
String
,
Object
>
result
=
new
HashMap
<
String
,
Object
>();
Map
<
String
,
Object
>
headData
=
new
HashMap
<
String
,
Object
>();
List
<
List
<
String
>>
rowData
=
new
ArrayList
<>();
try
{
// 根据文件路径创建缓冲输入流
br
=
new
BufferedReader
(
new
InputStreamReader
(
new
FileInputStream
(
fileFullPath
),
code
));
// 循环读取文件的每一行, 对需要修改的行进行修改, 放入缓冲对象中
int
i
=
1
;
while
((
line
=
br
.
readLine
())
!=
null
)
{
line
=
StringUtils
.
trim
(
line
);
if
(
StringUtils
.
isBlank
(
line
))
{
continue
;
}
int
index
=
StringUtils
.
indexOf
(
line
,
C_COLON
);
if
(
index
>
0
)
{
String
key
=
StringUtils
.
upperCase
(
StringUtils
.
trim
(
StringUtils
.
substringBefore
(
line
,
C_COLON
)));
String
value
=
StringUtils
.
trim
(
StringUtils
.
substringAfter
(
line
,
C_COLON
));
if
(
StringUtils
.
indexOf
(
value
,
C_COMMA
)
>
0
)
{
value
=
StringUtils
.
substringBefore
(
value
,
C_COMMA
);
}
headData
.
put
(
key
,
value
);
}
else
{
String
[]
value
=
StringUtils
.
splitPreserveAllTokens
(
line
,
C_COMMA
);
if
(
value
!=
null
&&
value
.
length
>
1
)
{
rowData
.
add
(
Arrays
.
asList
(
value
));
}
}
}
}
catch
(
Exception
e
)
{
// throw new SystemIllegalArgumentException(Errors.create().content("Read csv file fail!").build());
}
finally
{
// 关闭流
if
(
br
!=
null
)
{
try
{
br
.
close
();
}
catch
(
IOException
e
)
{
// logger.error(e);
System
.
out
.
println
(
e
);
br
=
null
;
}
}
}
result
.
put
(
"head"
,
headData
);
result
.
put
(
"body"
,
rowData
);
return
result
;
}
public
static
String
readTxtFile
(
String
fileFullPath
)
{
BufferedReader
br
=
null
;
String
line
;
// String code = judgeTextCode(fileFullPath);
String
code
=
"UTF-8"
;
StringBuilder
resultBuilder
=
new
StringBuilder
();
try
{
// 根据文件路径创建缓冲输入流
br
=
new
BufferedReader
(
new
InputStreamReader
(
new
FileInputStream
(
fileFullPath
),
code
));
while
((
line
=
br
.
readLine
())
!=
null
)
{
resultBuilder
.
append
(
StringUtils
.
trim
(
line
));
}
}
catch
(
Exception
e
)
{
// throw new SystemIllegalArgumentException(Errors.create().content("Read csv file fail!").build());
System
.
out
.
println
(
e
);
}
finally
{
// 关闭流
if
(
br
!=
null
)
{
try
{
br
.
close
();
}
catch
(
IOException
e
)
{
// logger.error(e);
System
.
out
.
println
(
e
);
br
=
null
;
}
}
}
return
resultBuilder
.
toString
();
}
private
static
String
judgeTextCode
(
String
strFilePath
)
{
FileInputStream
fis
=
null
;
String
code
=
""
;
try
{
fis
=
new
FileInputStream
(
strFilePath
);
int
a
=
fis
.
read
();
int
b
=
fis
.
read
();
if
(
a
==
0xFF
&&
b
==
0xFE
)
{
code
=
"Unicode"
;
}
else
if
(
a
==
0xFE
&&
b
==
0xFF
)
{
code
=
"UTF-16BE"
;
}
else
if
(
a
==
0xEF
&&
b
==
0xBB
)
{
code
=
"UTF-8"
;
}
else
{
code
=
"GBK"
;
}
}
catch
(
Exception
e
)
{
// throw new SystemIllegalArgumentException(Errors.create().content("Get file Code fail!").build());
}
finally
{
try
{
if
(
fis
!=
null
)
{
fis
.
close
();
}
}
catch
(
Exception
e
)
{
fis
=
null
;
}
}
return
code
;
}
/**
* 读取Excel数据
*
* @param fileFullPath 文件全路径
* @param sheetIndex 表索引,第一个表为0
* @return
*/
public
static
List
<
List
<
String
>>
readExcelFile
(
String
fileFullPath
,
int
sheetIndex
)
throws
IOException
{
try
{
String
excelType
=
getExcelType
(
fileFullPath
);
if
(
XLSX_TYPE
.
equals
(
excelType
))
{
return
initXLSX
(
getFile
(
fileFullPath
),
sheetIndex
,
excelType
);
}
else
if
(
XLS_TYPE
.
equals
(
excelType
))
{
return
initXLS
(
getFile
(
fileFullPath
),
sheetIndex
,
excelType
);
}
return
null
;
}
catch
(
Exception
e
)
{
// throw new SystemIllegalArgumentException(Errors.create().content("Read excel file fail!").build());
throw
e
;
}
}
private
static
List
<
List
<
String
>>
initXLS
(
FileInputStream
fileInputStream
,
int
sheetIndex
,
String
excelType
)
throws
IOException
{
Workbook
workbook
=
getHssfWorkbook
(
fileInputStream
);
Sheet
sheet
=
getHssfSheet
(
workbook
,
sheetIndex
);
int
totalRows
=
setTotalRows
(
sheet
);
return
getStringArrayList
(
sheet
,
excelType
,
totalRows
);
}
private
static
List
<
List
<
String
>>
initXLSX
(
FileInputStream
fileInputStream
,
int
sheetIndex
,
String
excelType
)
throws
IOException
{
// 获取Excel引用
Workbook
workbook
=
getXssfWorkbook
(
fileInputStream
);
// 获取Excel引用的第几张表
Sheet
sheet
=
getXssfSheet
(
workbook
,
sheetIndex
);
// 获取总行数
int
totalRows
=
setTotalRows
(
sheet
);
return
getStringArrayList
(
sheet
,
excelType
,
totalRows
);
}
/**
* String类型集合的List集合<br>
*
* @param sheet
* @param excelType 类型
* @param totalRows
* @return
*/
public
static
List
<
List
<
String
>>
getStringArrayList
(
Sheet
sheet
,
String
excelType
,
int
totalRows
)
{
List
<
List
<
String
>>
stringArrayList
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<
totalRows
;
i
++)
{
Row
row
=
isRowNotNull
(
sheet
,
excelType
,
i
);
if
(
row
!=
null
)
{
int
totalCells
=
setTotalCells
(
row
);
List
<
String
>
strings
=
new
ArrayList
<>();
for
(
int
j
=
0
;
j
<
totalCells
;
j
++)
{
strings
.
add
(
getCellStringValue
(
sheet
,
excelType
,
i
,
j
));
}
stringArrayList
.
add
(
strings
);
}
}
return
stringArrayList
;
}
/**
* 获取当前读取Excel文件类型
*
* @return XLS_TYPE or XLSX_TYPE
*/
private
static
String
getExcelType
(
String
filePath
)
{
return
StringUtils
.
endsWith
(
filePath
,
XLSX_TYPE
)
?
XLSX_TYPE
:
XLS_TYPE
;
}
/**
* 获取文件字节流
*
* @param filePath 文件路径
* @return FileInputStream
* @throws FileNotFoundException
*/
private
static
FileInputStream
getFile
(
String
filePath
)
throws
FileNotFoundException
{
return
new
FileInputStream
(
new
File
(
filePath
));
}
/**
* 获取第i行第j列的单元格的值 除日期类型,其他类型全处理为文本,与表格内容一致
*
* @param i 行号
* @param j 列号
* @return String 单元格中的数据
*/
public
static
String
getCellStringValue
(
Sheet
sheet
,
String
excelType
,
int
i
,
int
j
)
{
String
val
=
null
;
Cell
cell
=
getCell
(
sheet
,
excelType
,
i
,
j
);
if
(
cell
!=
null
)
{
if
(
cell
.
getCellTypeEnum
().
equals
(
CellType
.
NUMERIC
)
&&
DateUtil
.
isCellDateFormatted
(
cell
))
{
val
=
new
SimpleDateFormat
(
"yyyy-MM-dd"
).
format
(
cell
.
getDateCellValue
());
}
else
{
cell
.
setCellType
(
CellType
.
STRING
);
val
=
cell
.
toString
().
trim
();
}
}
return
val
==
null
?
""
:
val
.
trim
();
}
/**
* 获取第i行数据
*
* @param i 行号
* @return boolean ? true : false
*/
public
static
Row
isRowNotNull
(
Sheet
sheet
,
String
excelType
,
int
i
)
{
return
getRow
(
sheet
,
excelType
,
i
);
}
/**
* 获取Excel的行
*
* @param i 行号
* @return HSSFRow or XSSFRow
*/
public
static
Row
getRow
(
Sheet
sheet
,
String
excelType
,
int
i
)
{
return
XLSX_TYPE
.
equals
(
excelType
)
?
getXSSFRow
(
sheet
,
i
)
:
getHSSFRow
(
sheet
,
i
);
}
/**
* 获取Excel的列
*
* @param i 行号
* @param j 列号
* @return HSSFCell or XSSFCell
*/
public
static
Cell
getCell
(
Sheet
sheet
,
String
excelType
,
int
i
,
int
j
)
{
return
XLSX_TYPE
.
equals
(
excelType
)
?
getXSSFCell
(
sheet
,
i
,
j
)
:
getHSSFCell
(
sheet
,
i
,
j
);
}
/**
* 设置总行数
*
* @param sheet 表单引用 HSSFSheet or XSSFSheet
*/
private
static
int
setTotalRows
(
Sheet
sheet
)
{
return
sheet
.
getLastRowNum
()
+
1
;
}
/**
* 设置某行的总列数
*
* @param row 行引用 HSSFRow or XSSFRow
*/
private
static
int
setTotalCells
(
Row
row
)
{
return
row
.
getLastCellNum
();
}
/**
* XLSX类型的Excel引用
*
* @param file 文件字节流
* @return XSSFWorkbook
* @throws IOException
*/
private
static
XSSFWorkbook
getXssfWorkbook
(
InputStream
file
)
throws
IOException
{
return
new
XSSFWorkbook
(
file
);
}
/**
* XLSX类型的Excel表单
*
* @param sheetIndex 表单索引
* @return XSSFSheet
*/
private
static
XSSFSheet
getXssfSheet
(
Workbook
workbook
,
int
sheetIndex
)
{
return
(
XSSFSheet
)
workbook
.
getSheetAt
(
sheetIndex
);
}
/**
* XLSX类型的Excel表单中的行
*
* @param rowNum 行号
* @return XSSFRow
*/
private
static
XSSFRow
getXSSFRow
(
Sheet
sheet
,
int
rowNum
)
{
return
(
XSSFRow
)
sheet
.
getRow
(
rowNum
);
}
/**
* XLSX类型的Excel表单中的列
*
* @param rowNum 行号
* @param cellNum 列号
* @return XSSFCell
*/
private
static
XSSFCell
getXSSFCell
(
Sheet
sheet
,
int
rowNum
,
int
cellNum
)
{
return
getXSSFRow
(
sheet
,
rowNum
).
getCell
(
cellNum
);
}
/**
* XLS类型的Excel引用
*
* @param file 文件字节流
* @return HSSFWorkbook
* @throws IOException
*/
private
static
HSSFWorkbook
getHssfWorkbook
(
InputStream
file
)
throws
IOException
{
return
new
HSSFWorkbook
(
file
);
}
/**
* XLS类型的Excel表单
*
* @param sheetIndex 表单索引,第一个表为0
* @return HSSFSheet
*/
private
static
HSSFSheet
getHssfSheet
(
Workbook
workbook
,
int
sheetIndex
)
{
return
(
HSSFSheet
)
workbook
.
getSheetAt
(
sheetIndex
);
}
/**
* XLS类型的Excel表单中的行
*
* @param rowNum 行号
* @return HSSFRow
*/
private
static
HSSFRow
getHSSFRow
(
Sheet
sheet
,
int
rowNum
)
{
return
(
HSSFRow
)
sheet
.
getRow
(
rowNum
);
}
/**
* XLS类型的Excel表单中的列
*
* @param rowNum 行号
* @param cellNum 列号
* @return HSSFCell
*/
private
static
HSSFCell
getHSSFCell
(
Sheet
sheet
,
int
rowNum
,
int
cellNum
)
{
return
getHSSFRow
(
sheet
,
rowNum
).
getCell
(
cellNum
);
}
/**
* 移动指定文件夹下的文件
*
* @param files 需要移动的文件全路径名
* @param targetPath 移动文件的目标主路径
*/
public
static
void
moveFiles
(
Collection
<
String
>
files
,
String
targetPath
)
{
// logger.info(files);
System
.
out
.
println
(
files
);
files
.
forEach
(
filePath
->
{
File
file
=
new
File
(
filePath
);
if
(
file
!=
null
&&
file
.
isFile
())
{
String
target
=
targetPath
+
File
.
separator
+
StringUtils
.
substringAfterLast
(
filePath
,
File
.
separator
);
try
{
Files
.
move
(
Paths
.
get
(
filePath
),
Paths
.
get
(
target
),
StandardCopyOption
.
REPLACE_EXISTING
);
}
catch
(
IOException
e
)
{
// logger.error("moveFiles error:" + e);
System
.
out
.
println
(
"moveFiles error:"
+
e
);
}
}
});
}
/**
* 移动指定文件夹下的文件
*
* @param files 需要移动的文件全路径名
* @param targetPath 移动文件的目标主路径
*/
public
static
void
copyFiles
(
Collection
<
String
>
files
,
String
targetPath
)
{
// logger.info(files);
files
.
forEach
(
filePath
->
{
File
file
=
new
File
(
filePath
);
if
(
file
!=
null
&&
file
.
isFile
())
{
String
target
=
targetPath
+
File
.
separator
+
StringUtils
.
substringAfterLast
(
filePath
,
File
.
separator
);
try
{
Files
.
copy
(
Paths
.
get
(
filePath
),
Paths
.
get
(
target
),
StandardCopyOption
.
REPLACE_EXISTING
);
}
catch
(
IOException
e
)
{
// logger.error("copyFiles error:" + e);
System
.
out
.
println
(
"moveFiles error:"
+
e
);
}
}
});
}
}
src/main/java/utils/ThirdApiCaller.java
0 → 100644
View file @
43cde115
package
utils
;
import
org.springframework.http.HttpEntity
;
import
org.springframework.http.HttpHeaders
;
import
org.springframework.http.converter.HttpMessageConverter
;
import
org.springframework.http.converter.StringHttpMessageConverter
;
import
org.springframework.util.LinkedMultiValueMap
;
import
org.springframework.web.client.RestTemplate
;
import
java.nio.charset.StandardCharsets
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
/**
* @author JP
* @createTime 2021/9/23
*/
public
class
ThirdApiCaller
{
private
static
final
String
FORM_CONTENT_TYPE
=
"application/x-www-form-urlencoded"
;
private
static
final
String
JSON_CONTENT_TYPE
=
"application/json;charset=utf8"
;
private
static
final
String
XML_CONTENT_TYPE
=
"application/xml;charset=utf8"
;
private
static
RestTemplate
REST_TEMPLATE
;
static
{
RestTemplate
restTemplate
=
new
RestTemplate
();
List
<
HttpMessageConverter
<?>>
list
=
restTemplate
.
getMessageConverters
();
for
(
HttpMessageConverter
<?>
httpMessageConverter:
list
){
if
(
httpMessageConverter
instanceof
StringHttpMessageConverter
){
((
StringHttpMessageConverter
)
httpMessageConverter
).
setDefaultCharset
(
StandardCharsets
.
UTF_8
);
}
}
REST_TEMPLATE
=
restTemplate
;
}
/**
* FORM形式
*
* @param url
* @param body
* @param header
* @param clazz
* @param <T>
* @return
*/
public
static
<
T
>
T
postForm
(
String
url
,
LinkedMultiValueMap
<
String
,
?
extends
Object
>
body
,
Map
<
String
,
String
>
header
,
Class
<
T
>
clazz
)
{
try
{
HttpHeaders
httpHeaders
=
createFormHttpHeaders
(
header
);
body
=
body
==
null
?
new
LinkedMultiValueMap
<
String
,
Object
>()
:
body
;
return
REST_TEMPLATE
.
postForObject
(
url
,
new
HttpEntity
(
body
,
httpHeaders
),
clazz
);
}
catch
(
Exception
e
)
{
throw
e
;
}
}
/**
* JSON形式
*
* @param url
* @param body
* @param header
* @param clazz
* @param <T>
* @return
*/
public
static
<
T
>
T
postJson
(
String
url
,
Object
body
,
Map
<
String
,
String
>
header
,
Class
<
T
>
clazz
)
{
try
{
HttpHeaders
httpHeaders
=
createJsonHttpHeaders
(
header
);
body
=
body
==
null
?
new
HashMap
<
String
,
String
>()
:
body
;
return
REST_TEMPLATE
.
postForObject
(
url
,
new
HttpEntity
(
body
,
httpHeaders
),
clazz
);
}
catch
(
Exception
e
)
{
throw
e
;
}
}
public
static
<
T
>
T
postXml
(
String
url
,
Object
body
,
Map
<
String
,
String
>
header
,
Class
<
T
>
clazz
)
{
try
{
HttpHeaders
httpHeaders
=
createXmlHttpHeaders
(
header
);
body
=
body
==
null
?
new
HashMap
<
String
,
String
>()
:
body
;
return
REST_TEMPLATE
.
postForObject
(
url
,
new
HttpEntity
(
body
,
httpHeaders
),
clazz
);
}
catch
(
Exception
e
)
{
throw
e
;
}
}
private
static
HttpHeaders
createFormHttpHeaders
(
Map
<
String
,
String
>
header
)
{
HttpHeaders
headers
=
new
HttpHeaders
();
if
(
header
!=
null
)
{
for
(
Map
.
Entry
<
String
,
String
>
entry
:
header
.
entrySet
())
{
headers
.
add
(
entry
.
getKey
(),
entry
.
getValue
());
}
}
headers
.
add
(
"Content-Type"
,
FORM_CONTENT_TYPE
);
return
headers
;
}
private
static
HttpHeaders
createJsonHttpHeaders
(
Map
<
String
,
String
>
header
)
{
HttpHeaders
headers
=
new
HttpHeaders
();
if
(
header
!=
null
)
{
for
(
Map
.
Entry
<
String
,
String
>
entry
:
header
.
entrySet
())
{
headers
.
add
(
entry
.
getKey
(),
entry
.
getValue
());
}
}
headers
.
add
(
"Content-Type"
,
JSON_CONTENT_TYPE
);
return
headers
;
}
private
static
HttpHeaders
createXmlHttpHeaders
(
Map
<
String
,
String
>
header
)
{
HttpHeaders
headers
=
new
HttpHeaders
();
if
(
header
!=
null
)
{
for
(
Map
.
Entry
<
String
,
String
>
entry
:
header
.
entrySet
())
{
headers
.
add
(
entry
.
getKey
(),
entry
.
getValue
());
}
}
headers
.
add
(
"Content-Type"
,
XML_CONTENT_TYPE
);
return
headers
;
}
}
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