Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
P
pdstask
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
曲欣亮
pdstask
Commits
1ce3c170
Commit
1ce3c170
authored
Jul 16, 2019
by
Quxl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
x
parent
4cee6888
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
75 additions
and
84 deletions
+75
-84
HttpUtil.java
src/main/java/com/egolm/pds/config/HttpUtil.java
+42
-79
PdsProductsUpdateTask.java
...in/java/com/egolm/pds/schedule/PdsProductsUpdateTask.java
+7
-1
PdsService.java
src/main/java/com/egolm/pds/schedule/service/PdsService.java
+26
-4
No files found.
src/main/java/com/egolm/pds/config/HttpUtil.java
View file @
1ce3c170
package
com
.
egolm
.
pds
.
config
;
import
java.io.ByteArrayOutputStream
;
import
java.io.File
;
import
java.io.FileInputStream
;
import
java.io.IOException
;
import
java.io.OutputStream
;
import
java.io.DataOutputStream
;
import
java.io.UnsupportedEncodingException
;
import
java.net.HttpURLConnection
;
import
java.net.URL
;
import
java.net.URLEncoder
;
import
java.util.ArrayList
;
import
java.util.List
;
import
java.util.Map
;
import
java.util.regex.Matcher
;
import
java.util.regex.Pattern
;
import
com.egolm.common.FileUtil
;
import
com.egolm.common.StringUtil
;
import
com.egolm.common.exception.HttpUrlEncodingException
;
public
class
HttpUtil
{
...
...
@@ -76,40 +77,20 @@ public class HttpUtil {
}
}
public
static
String
post
(
String
requestUrl
,
Map
<
String
,
String
>
parameters
,
Map
<
String
,
String
>
headers
)
{
public
static
String
post
(
String
requestUrl
,
Map
<
String
,
Object
>
parameters
,
Map
<
String
,
String
>
headers
)
{
HttpURLConnection
connection
=
null
;
try
{
Data
data
=
new
Data
();
String
BOUNDARY
=
"----WebKitFormBoundaryT1HoybnYeFOGFlBR"
;
StringBuffer
ParamBuffer
=
new
StringBuffer
();
if
(
parameters
!=
null
)
{
for
(
String
key
:
parameters
.
keySet
())
{
ParamBuffer
.
append
(
"--"
+
BOUNDARY
+
"\r\n"
);
ParamBuffer
.
append
(
"Content-Disposition: form-data; name=\""
+
key
+
"\"\r\n"
);
ParamBuffer
.
append
(
"\r\n"
);
ParamBuffer
.
append
(
parameters
.
get
(
key
)
+
"\r\n"
);
}
}
ParamBuffer
.
append
(
"--"
+
BOUNDARY
+
"\r\nContent-Disposition: form-data; name=\"----------------------------------\"\r\n\r\n-------------------------\r\n"
);
String
ParamBufferString
=
ParamBuffer
.
toString
();
data
.
add
(
ParamBufferString
.
getBytes
());
StringBuffer
EndBuffer
=
new
StringBuffer
(
"\r\n--"
+
BOUNDARY
+
"--\r\n"
);
String
EndBufferString
=
EndBuffer
.
toString
();
data
.
add
(
EndBufferString
.
getBytes
());
URL
url
=
new
URL
(
requestUrl
);
connection
=
(
HttpURLConnection
)
url
.
openConnection
();
connection
.
setRequestMethod
(
"POST"
);
connection
.
setRequestProperty
(
"Accept-Charset"
,
"utf-8"
);
connection
.
setRequestProperty
(
"Content-Type"
,
"application/json; boundary="
+
BOUNDARY
);
connection
.
setRequestProperty
(
"Content-Length"
,
String
.
valueOf
(
data
.
length
()));
if
(
headers
!=
null
)
{
for
(
String
key
:
headers
.
keySet
())
{
connection
.
setRequestProperty
(
key
,
headers
.
get
(
key
));
}
}
connection
=
(
HttpURLConnection
)
new
URL
(
requestUrl
).
openConnection
();
connection
.
setDoOutput
(
true
);
OutputStream
out
=
connection
.
getOutputStream
();
out
.
write
(
data
.
bytes
());
connection
.
setDoInput
(
true
);
connection
.
setRequestMethod
(
"POST"
);
connection
.
setUseCaches
(
false
);
connection
.
setInstanceFollowRedirects
(
true
);
connection
.
setRequestProperty
(
"Content-Type"
,
"application/json"
);
connection
.
connect
();
DataOutputStream
out
=
new
DataOutputStream
(
connection
.
getOutputStream
());
out
.
write
(
toQueryString
(
parameters
).
getBytes
());
out
.
flush
();
out
.
close
();
return
responseBody
(
connection
);
}
catch
(
Exception
e
)
{
...
...
@@ -118,50 +99,32 @@ public class HttpUtil {
}
}
public
static
class
Data
{
private
byte
[][]
ds
=
new
byte
[
0
][
0
];
public
void
add
(
File
file
)
throws
IOException
{
FileInputStream
fis
=
new
FileInputStream
(
file
);
ByteArrayOutputStream
bos
=
new
ByteArrayOutputStream
(
1000
);
byte
[]
b
=
new
byte
[
1000
];
int
n
;
while
((
n
=
fis
.
read
(
b
))
!=
-
1
)
{
bos
.
write
(
b
,
0
,
n
);
}
fis
.
close
();
bos
.
close
();
add
(
bos
.
toByteArray
());
}
public
void
add
(
byte
[]
data
)
{
int
length
=
ds
.
length
;
byte
[][]
ds_tmp
=
new
byte
[
length
+
1
][];
for
(
int
i
=
0
;
i
<
length
;
i
++)
{
ds_tmp
[
i
]
=
ds
[
i
];
}
ds_tmp
[
length
]
=
data
;
ds
=
ds_tmp
;
public
static
String
toQueryString
(
Map
<?,
?>
parameters
)
{
return
toQueryString
(
parameters
,
null
);
}
public
int
length
()
{
int
length
=
0
;
for
(
byte
[]
b
:
ds
)
{
length
+=
b
.
length
;
public
static
String
toQueryString
(
Map
<?,
?>
parameters
,
String
encode
)
throws
HttpUrlEncodingException
{
try
{
List
<
String
>
params
=
new
ArrayList
<
String
>();
if
(
parameters
!=
null
)
{
for
(
Object
key
:
parameters
.
keySet
())
{
Object
val
=
parameters
.
get
(
key
);
String
sKey
=
String
.
valueOf
(
key
);
Object
[]
sVals
=
(
val
==
null
?
null
:
(
val
instanceof
Object
[]
?
(
Object
[])
val
:
new
Object
[]
{
val
}));
if
(
sVals
!=
null
&&
sVals
.
length
>
0
)
{
for
(
Object
sVal
:
sVals
)
{
params
.
add
(
sKey
+
"="
+
(
sVal
==
null
?
""
:
URLEncoder
.
encode
(
String
.
valueOf
(
sVal
),
encode
==
null
?
"utf-8"
:
encode
)));
}
return
length
;
}
else
{
params
.
add
(
"sKey="
);
}
public
byte
[]
bytes
()
{
byte
[]
bytes
=
new
byte
[
length
()];
int
index
=
0
;
for
(
int
i
=
0
;
i
<
ds
.
length
;
i
++)
{
for
(
int
k
=
0
;
k
<
ds
[
i
].
length
;
k
++)
{
bytes
[
index
++]
=
ds
[
i
][
k
];
}
}
return
bytes
;
return
StringUtil
.
join
(
"&"
,
params
);
}
catch
(
UnsupportedEncodingException
e
)
{
throw
new
HttpUrlEncodingException
(
"URL编码异常"
,
e
);
}
}
}
src/main/java/com/egolm/pds/schedule/PdsProductsUpdateTask.java
View file @
1ce3c170
package
com
.
egolm
.
pds
.
schedule
;
import
java.util.ArrayList
;
import
java.util.Date
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -50,6 +52,8 @@ public class PdsProductsUpdateTask {
System
.
out
.
println
(
responseBody
);
JSONObject
jsonObject
=
JSON
.
parseObject
(
responseBody
);
JSONArray
jsonArray
=
jsonObject
.
getJSONArray
(
"data"
);
List
<
Object
>
idList
=
new
ArrayList
<
Object
>();
List
<
Map
<
String
,
Object
>>
list
=
new
ArrayList
<
Map
<
String
,
Object
>>();
if
(
jsonArray
!=
null
)
{
for
(
int
i
=
0
;
i
<
jsonArray
.
size
();
i
++)
{
JSONObject
productJsonObject
=
jsonArray
.
getJSONObject
(
i
);
...
...
@@ -97,9 +101,11 @@ public class PdsProductsUpdateTask {
productMap
.
put
(
"trade_mode"
,
trade_mode
);
productMap
.
put
(
"weight_article"
,
weight_article
);
productMap
.
put
(
"dLastUpdateTime"
,
new
Date
());
pdsService
.
savePdsProduct
(
transactionId
,
productMap
);
list
.
add
(
productMap
);
idList
.
add
(
transactionId
);
}
}
pdsService
.
savePdsProduct
(
list
,
idList
);;
return
jsonArray
==
null
?
0
:
jsonArray
.
size
();
}
...
...
src/main/java/com/egolm/pds/schedule/service/PdsService.java
View file @
1ce3c170
package
com
.
egolm
.
pds
.
schedule
.
service
;
import
java.util.ArrayList
;
import
java.util.HashMap
;
import
java.util.List
;
import
java.util.Map
;
import
org.springframework.beans.factory.annotation.Autowired
;
...
...
@@ -8,6 +10,7 @@ import org.springframework.beans.factory.annotation.Value;
import
org.springframework.stereotype.Component
;
import
org.springframework.transaction.annotation.Transactional
;
import
com.egolm.common.StringUtil
;
import
com.egolm.common.jdbc.JdbcTemplate
;
import
com.egolm.pds.config.HttpUtil
;
...
...
@@ -27,13 +30,29 @@ public class PdsService {
private
String
authValue
;
@Transactional
public
void
savePdsProduct
(
Object
transactionId
,
Map
<
String
,
Object
>
productMap
)
{
jdbcTemplate
.
save
(
"pds_article"
,
productMap
);
Map
<
String
,
String
>
parameters
=
new
HashMap
<
String
,
String
>();
public
void
savePdsProduct
(
List
<
Map
<
String
,
Object
>>
list
,
List
<
Object
>
ids
)
{
List
<
String
>
columns
=
new
ArrayList
<
String
>();
if
(
list
!=
null
&&
list
.
size
()
>
0
)
{
Map
<
String
,
Object
>
first
=
list
.
get
(
0
);
for
(
String
key
:
first
.
keySet
())
{
columns
.
add
(
key
);
}
}
List
<
Object
[]>
args
=
new
ArrayList
<
Object
[]>();
String
sql
=
"insert into pds_article ("
+
StringUtil
.
join
(
", "
,
columns
)
+
") values ("
+
StringUtil
.
join
(
"?"
,
", "
,
columns
.
size
(),
""
,
""
)
+
")"
;
for
(
Map
<
String
,
Object
>
map
:
list
)
{
List
<
Object
>
argsAry
=
new
ArrayList
<
Object
>();
for
(
String
column
:
columns
)
{
argsAry
.
add
(
map
.
get
(
column
));
}
args
.
add
(
argsAry
.
toArray
());
}
jdbcTemplate
.
batchUpdate
(
sql
,
args
);
Map
<
String
,
Object
>
parameters
=
new
HashMap
<
String
,
Object
>();
Map
<
String
,
String
>
headers
=
new
HashMap
<
String
,
String
>();
headers
.
put
(
authKey
,
authValue
);
parameters
.
put
(
"transaction_id"
,
transactionId
==
null
?
null
:
transactionId
.
toString
());
HttpUtil
.
post
(
urlUpdate
,
parameters
,
headers
);
}
public
JdbcTemplate
getJdbcTemplate
()
{
...
...
@@ -44,4 +63,7 @@ public class PdsService {
this
.
jdbcTemplate
=
jdbcTemplate
;
}
}
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