Commit 1ce3c170 authored by Quxl's avatar Quxl

x

parent 4cee6888
package com.egolm.pds.config; package com.egolm.pds.config;
import java.io.ByteArrayOutputStream; import java.io.DataOutputStream;
import java.io.File; import java.io.UnsupportedEncodingException;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URL; import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import com.egolm.common.FileUtil; import com.egolm.common.FileUtil;
import com.egolm.common.StringUtil;
import com.egolm.common.exception.HttpUrlEncodingException;
public class HttpUtil { public class HttpUtil {
...@@ -76,40 +77,20 @@ 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; HttpURLConnection connection = null;
try { try {
Data data = new Data(); connection = (HttpURLConnection) new URL(requestUrl).openConnection();
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.setDoOutput(true); connection.setDoOutput(true);
OutputStream out = connection.getOutputStream(); connection.setDoInput(true);
out.write(data.bytes()); 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(); out.close();
return responseBody(connection); return responseBody(connection);
} catch (Exception e) { } catch (Exception e) {
...@@ -118,50 +99,32 @@ public class HttpUtil { ...@@ -118,50 +99,32 @@ public class HttpUtil {
} }
} }
public static class Data { public static String toQueryString(Map<?, ?> parameters) {
return toQueryString(parameters, null);
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 int length() { public static String toQueryString(Map<?, ?> parameters, String encode) throws HttpUrlEncodingException {
int length = 0; try {
for (byte[] b : ds) { List<String> params = new ArrayList<String>();
length += b.length; 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);
} }
} }
} }
package com.egolm.pds.schedule; package com.egolm.pds.schedule;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -50,6 +52,8 @@ public class PdsProductsUpdateTask { ...@@ -50,6 +52,8 @@ public class PdsProductsUpdateTask {
System.out.println(responseBody); System.out.println(responseBody);
JSONObject jsonObject = JSON.parseObject(responseBody); JSONObject jsonObject = JSON.parseObject(responseBody);
JSONArray jsonArray = jsonObject.getJSONArray("data"); JSONArray jsonArray = jsonObject.getJSONArray("data");
List<Object> idList = new ArrayList<Object>();
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
if(jsonArray != null) { if(jsonArray != null) {
for(int i = 0; i < jsonArray.size(); i++) { for(int i = 0; i < jsonArray.size(); i++) {
JSONObject productJsonObject = jsonArray.getJSONObject(i); JSONObject productJsonObject = jsonArray.getJSONObject(i);
...@@ -97,9 +101,11 @@ public class PdsProductsUpdateTask { ...@@ -97,9 +101,11 @@ public class PdsProductsUpdateTask {
productMap.put("trade_mode", trade_mode); productMap.put("trade_mode", trade_mode);
productMap.put("weight_article", weight_article); productMap.put("weight_article", weight_article);
productMap.put("dLastUpdateTime", new Date()); 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(); return jsonArray == null ? 0 : jsonArray.size();
} }
......
package com.egolm.pds.schedule.service; package com.egolm.pds.schedule.service;
import java.util.ArrayList;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -8,6 +10,7 @@ import org.springframework.beans.factory.annotation.Value; ...@@ -8,6 +10,7 @@ import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import com.egolm.common.StringUtil;
import com.egolm.common.jdbc.JdbcTemplate; import com.egolm.common.jdbc.JdbcTemplate;
import com.egolm.pds.config.HttpUtil; import com.egolm.pds.config.HttpUtil;
...@@ -27,13 +30,29 @@ public class PdsService { ...@@ -27,13 +30,29 @@ public class PdsService {
private String authValue; private String authValue;
@Transactional @Transactional
public void savePdsProduct(Object transactionId, Map<String, Object> productMap) { public void savePdsProduct(List<Map<String, Object>> list, List<Object> ids) {
jdbcTemplate.save("pds_article", productMap); List<String> columns = new ArrayList<String>();
Map<String, String> parameters = new HashMap<String, 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>(); Map<String, String> headers = new HashMap<String, String>();
headers.put(authKey, authValue); headers.put(authKey, authValue);
parameters.put("transaction_id", transactionId == null ? null : transactionId.toString());
HttpUtil.post(urlUpdate, parameters, headers); HttpUtil.post(urlUpdate, parameters, headers);
} }
public JdbcTemplate getJdbcTemplate() { public JdbcTemplate getJdbcTemplate() {
...@@ -44,4 +63,7 @@ public class PdsService { ...@@ -44,4 +63,7 @@ public class PdsService {
this.jdbcTemplate = jdbcTemplate; this.jdbcTemplate = jdbcTemplate;
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment