Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
common
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
曲欣亮
common
Commits
4f36c623
Commit
4f36c623
authored
Aug 17, 2021
by
张永
Browse files
Options
Browse Files
Download
Plain Diff
Merge branch 'master' of
http://gitlab.linkfern.com/key/common.git
parents
6b24f32a
8190468a
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
220 additions
and
12 deletions
+220
-12
pom.xml
pom.xml
+8
-3
FileUtil.java
src/main/java/com/egolm/common/FileUtil.java
+23
-8
HttpUtil.java
src/main/java/com/egolm/common/HttpUtil.java
+1
-1
DBPort.java
src/main/java/com/egolm/common/jdbc/DBPort.java
+188
-0
No files found.
pom.xml
View file @
4f36c623
...
...
@@ -95,9 +95,14 @@
<version>
4.5.2
</version>
</dependency>
<dependency>
<groupId>
commons-httpclient
</groupId>
<artifactId>
commons-httpclient
</artifactId>
<version>
3.1
</version>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpclient
</artifactId>
<version>
4.5.13
</version>
</dependency>
<dependency>
<groupId>
org.apache.httpcomponents
</groupId>
<artifactId>
httpmime
</artifactId>
<version>
4.5.13
</version>
</dependency>
<dependency>
<groupId>
com.sun.mail
</groupId>
...
...
src/main/java/com/egolm/common/FileUtil.java
View file @
4f36c623
...
...
@@ -426,24 +426,39 @@ public class FileUtil {
OutputStreamWriter
osw
=
null
;
Writer
writer
=
null
;
try
{
if
(!
file
.
getParentFile
().
exists
())
{
file
.
getParentFile
().
mkdirs
();
File
absFile
=
file
.
getAbsoluteFile
();
if
(!
absFile
.
getParentFile
().
exists
())
{
absFile
.
getParentFile
().
mkdirs
();
}
fos
=
new
FileOutputStream
(
f
ile
);
fos
=
new
FileOutputStream
(
absF
ile
);
osw
=
new
OutputStreamWriter
(
fos
,
"UTF-8"
);
writer
=
new
BufferedWriter
(
osw
);
for
(
String
string
:
strings
)
{
writer
.
append
(
string
).
append
(
System
.
getProperty
(
"line.separator"
));
}
}
catch
(
IOException
e
)
{
throw
new
FileUtilException
(
e
);
throw
new
FileUtilException
(
""
,
e
);
}
finally
{
try
{
writer
.
close
();
osw
.
close
();
fos
.
close
();
if
(
writer
!=
null
)
{
writer
.
close
();
}
}
catch
(
IOException
e
)
{
throw
new
FileUtilException
(
e
);
e
.
printStackTrace
();
}
try
{
if
(
osw
!=
null
)
{
osw
.
close
();
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
try
{
if
(
fos
!=
null
)
{
fos
.
close
();
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
...
...
src/main/java/com/egolm/common/HttpUtil.java
View file @
4f36c623
...
...
@@ -70,7 +70,7 @@ public class HttpUtil {
return
sslContextDefault
;
}
p
rivate
static
HttpURLConnection
createConnection
(
String
requestUrl
,
Proxy
proxy
)
throws
IOException
{
p
ublic
static
HttpURLConnection
createConnection
(
String
requestUrl
,
Proxy
proxy
)
throws
IOException
{
URL
HTTP_URL
=
new
URL
(
requestUrl
);
HttpURLConnection
connection
=
(
HttpURLConnection
)
(
proxy
==
null
?
HTTP_URL
.
openConnection
()
:
HTTP_URL
.
openConnection
(
proxy
));
if
(
requestUrl
.
startsWith
(
"https"
))
{
...
...
src/main/java/com/egolm/common/jdbc/DBPort.java
0 → 100644
View file @
4f36c623
package
com
.
egolm
.
common
.
jdbc
;
import
java.io.ByteArrayInputStream
;
import
java.io.ByteArrayOutputStream
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.FileOutputStream
;
import
java.io.IOException
;
import
java.io.ObjectInputStream
;
import
java.io.ObjectOutputStream
;
import
java.io.OutputStream
;
import
java.nio.ByteBuffer
;
import
java.nio.channels.FileChannel
;
import
java.sql.Connection
;
import
java.sql.PreparedStatement
;
import
java.sql.ResultSet
;
import
java.sql.ResultSetMetaData
;
import
java.sql.SQLException
;
import
java.sql.Timestamp
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.LinkedHashMap
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.zip.GZIPInputStream
;
import
java.util.zip.GZIPOutputStream
;
import
javax.sql.DataSource
;
public
class
DBPort
{
private
DataSource
dataSource
;
public
DBPort
(
DataSource
dataSource
)
{
this
.
dataSource
=
dataSource
;
}
public
void
doImport
(
File
exportFile
,
String
tableName
)
throws
IOException
,
SQLException
,
ClassNotFoundException
{
FileInputStream
fis
=
null
;
try
{
fis
=
new
FileInputStream
(
exportFile
);
FileChannel
channel
=
fis
.
getChannel
();
ByteBuffer
byteBuffer
=
ByteBuffer
.
allocate
((
int
)
channel
.
size
());
int
i
=
1
;
while
(
i
>
0
)
{
i
=
channel
.
read
(
byteBuffer
);
}
this
.
doImport
(
byteBuffer
.
array
(),
tableName
);
}
finally
{
if
(
fis
!=
null
)
{
fis
.
close
();
}
}
}
public
void
doExport
(
File
exportFile
,
String
selectSql
,
Object
[]
args
)
throws
IOException
,
SQLException
{
if
(!
exportFile
.
getParentFile
().
exists
())
{
exportFile
.
getParentFile
().
mkdirs
();
}
FileOutputStream
fos
=
new
FileOutputStream
(
exportFile
);
this
.
doExport
(
fos
,
selectSql
,
args
);
}
public
void
doExport
(
OutputStream
os
,
String
sql
,
Object
[]
args
)
throws
IOException
,
SQLException
{
try
{
os
.
write
(
this
.
doExport
(
sql
,
args
));
os
.
flush
();
}
finally
{
if
(
os
!=
null
)
{
os
.
close
();
}
}
}
public
byte
[]
doExport
(
String
selectSql
,
Object
[]
args
)
throws
IOException
,
SQLException
{
Connection
connection
=
dataSource
.
getConnection
();
PreparedStatement
ps
=
connection
.
prepareStatement
(
selectSql
);
if
(
args
!=
null
)
{
for
(
int
i
=
0
;
i
<
args
.
length
;
i
++)
{
Object
argObj
=
args
[
i
];
if
(
argObj
instanceof
java
.
util
.
Date
)
{
ps
.
setObject
(
i
+
1
,
new
Timestamp
(((
Date
)
argObj
).
getTime
()));
}
else
if
(
argObj
.
getClass
().
isEnum
())
{
ps
.
setObject
(
i
+
1
,
args
[
i
].
toString
());
}
else
{
ps
.
setObject
(
i
+
1
,
args
[
i
]);
}
}
}
ResultSet
resultSet
=
ps
.
executeQuery
();
ResultSetMetaData
metaData
=
resultSet
.
getMetaData
();
int
columnCount
=
metaData
.
getColumnCount
();
ArrayList
<
Map
<
String
,
Map
<
String
,
Object
>>>
datas
=
new
ArrayList
<
Map
<
String
,
Map
<
String
,
Object
>>>();
while
(
resultSet
.
next
())
{
Map
<
String
,
Map
<
String
,
Object
>>
rowData
=
new
LinkedHashMap
<
String
,
Map
<
String
,
Object
>>();
for
(
int
i
=
1
;
i
<=
columnCount
;
i
++)
{
String
colType
=
metaData
.
getColumnTypeName
(
i
);
String
columnLabel
=
metaData
.
getColumnLabel
(
i
);
if
(
columnLabel
==
null
||
columnLabel
.
trim
().
length
()
==
0
)
{
columnLabel
=
metaData
.
getColumnName
(
i
);
}
Map
<
String
,
Object
>
map
=
new
HashMap
<
String
,
Object
>();
map
.
put
(
"type"
,
colType
);
map
.
put
(
"value"
,
resultSet
.
getObject
(
i
));
rowData
.
put
(
columnLabel
,
map
);
}
datas
.
add
(
rowData
);
}
ByteArrayOutputStream
bos
=
null
;
GZIPOutputStream
gzip
=
null
;
ObjectOutputStream
out
=
null
;
try
{
bos
=
new
ByteArrayOutputStream
();
gzip
=
new
GZIPOutputStream
(
bos
);
out
=
new
ObjectOutputStream
(
gzip
);
out
.
writeObject
(
datas
);
out
.
flush
();
gzip
.
flush
();
gzip
.
finish
();
bos
.
flush
();
return
bos
.
toByteArray
();
}
finally
{
if
(
out
!=
null
)
{
try
{
out
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
if
(
bos
!=
null
)
{
try
{
bos
.
close
();
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
}
}
}
public
void
doImport
(
byte
[]
data
,
String
tableName
)
throws
IOException
,
ClassNotFoundException
,
SQLException
{
ByteArrayInputStream
bis
=
new
ByteArrayInputStream
(
data
);
GZIPInputStream
gzip
=
new
GZIPInputStream
(
bis
);
ObjectInputStream
out
=
new
ObjectInputStream
(
gzip
);
List
<?>
datas
=
(
List
<?>)
out
.
readObject
();
StringBuffer
sql
=
new
StringBuffer
(
"insert into "
).
append
(
tableName
).
append
(
"("
);
Object
first
=
datas
.
get
(
0
);
Map
<?,
?>
firstMap
=
(
Map
<?,
?>)
first
;
int
columnIndex
=
0
;
for
(
Object
keyObject
:
firstMap
.
keySet
())
{
String
columnName
=
(
String
)
keyObject
;
sql
.
append
(
columnName
);
if
(
columnIndex
<
firstMap
.
size
()
-
1
)
{
sql
.
append
(
", "
);
}
else
{
sql
.
append
(
") values ("
);
for
(
int
k
=
0
;
k
<
firstMap
.
size
();
k
++)
{
sql
.
append
(
"?"
);
if
(
k
<
firstMap
.
size
()
-
1
)
{
sql
.
append
(
", "
);
}
else
{
sql
.
append
(
")"
);
}
}
}
columnIndex
+=
1
;
}
Connection
connection
=
dataSource
.
getConnection
();
connection
.
setAutoCommit
(
false
);
PreparedStatement
ps
=
connection
.
prepareStatement
(
sql
.
toString
());
for
(
int
i
=
0
;
i
<
datas
.
size
();
i
++)
{
Object
obj
=
datas
.
get
(
i
);
Map
<?,
?>
map
=
(
Map
<?,
?>)
obj
;
int
index
=
0
;
for
(
Object
keyObject
:
map
.
keySet
())
{
index
+=
1
;
Object
colObj
=
map
.
get
(
keyObject
);
Map
<?,
?>
columnMap
=
(
Map
<?,
?>)
colObj
;
Object
columnValue
=
columnMap
.
get
(
"value"
);
ps
.
setObject
(
index
,
columnValue
);
}
ps
.
addBatch
();
}
ps
.
executeUpdate
();
connection
.
commit
();
}
}
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