Commit b83cdf6a authored by Quxl's avatar Quxl

x

parent 7de2432f
package com.egolm.pds.config; package com.egolm.pds.config;
import java.io.DataOutputStream; import java.io.ByteArrayOutputStream;
import java.io.UnsupportedEncodingException; import java.io.File;
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 {
...@@ -77,54 +76,115 @@ public class HttpUtil { ...@@ -77,54 +76,115 @@ public class HttpUtil {
} }
} }
public static String post(String requestUrl, Map<String, Object> parameters, Map<String, String> headers) { public static String post(String requestUrl, Map<String, String> parameters, Map<String, String> headers) {
return post(requestUrl, parameters, headers, null);
}
public static String post(String requestUrl, Map<String, String> parameters, Map<String, String> headers,
Map<String, File> attachments) {
HttpURLConnection connection = null; HttpURLConnection connection = null;
try { try {
connection = (HttpURLConnection) new URL(requestUrl).openConnection(); Data data = new Data();
connection.setDoOutput(true); String BOUNDARY = "----WebKitFormBoundaryT1HoybnYeFOGFlBR";
connection.setDoInput(true); StringBuffer ParamBuffer = new StringBuffer();
connection.setRequestMethod("POST"); if (parameters != null) {
connection.setUseCaches(false); for (String key : parameters.keySet()) {
connection.setInstanceFollowRedirects(true); ParamBuffer.append("--" + BOUNDARY + "\r\n");
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); ParamBuffer.append("Content-Disposition: form-data; name=\"" + key + "\"\r\n");
connection.connect(); ParamBuffer.append("\r\n");
DataOutputStream out = new DataOutputStream(connection.getOutputStream()); ParamBuffer.append(parameters.get(key) + "\r\n");
out.write(toQueryString(parameters).getBytes()); }
out.flush(); }
out.close(); ParamBuffer.append("--" + BOUNDARY
+ "\r\nContent-Disposition: form-data; name=\"----------------------------------\"\r\n\r\n-------------------------\r\n");
String ParamBufferString = ParamBuffer.toString();
data.add(ParamBufferString.getBytes());
if (attachments != null) {
for (String name : attachments.keySet()) {
StringBuffer FileBuffer = new StringBuffer();
File file = attachments.get(name);
FileBuffer.append("--" + BOUNDARY + "\r\n");
FileBuffer.append("Content-Disposition: form-data; name=\"" + name + "\"; filename=\""
+ file.getName() + "\"" + "\r\n");
FileBuffer.append("Content-Type: application/octet-stream" + "\r\n");
FileBuffer.append("\r\n");
String FileBufferString = FileBuffer.toString();
data.add(FileBufferString.getBytes());
data.add(file);
String FileEnd = "\r\n";
data.add(FileEnd.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", "multipart/form-data; 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);
OutputStream out = connection.getOutputStream();
out.write(data.bytes());
out.close();
return responseBody(connection); return responseBody(connection);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e); throw new RuntimeException(e);
} finally {
connection.disconnect();
} }
} }
public static String toQueryString(Map<?, ?> parameters) { public static class Data {
return toQueryString(parameters, null);
}
public static String toQueryString(Map<?, ?> parameters, String encode) throws HttpUrlEncodingException { private byte[][] ds = new byte[0][0];
try {
List<String> params = new ArrayList<String>(); public void add(File file) throws IOException {
if (parameters != null) { FileInputStream fis = new FileInputStream(file);
for (Object key : parameters.keySet()) { ByteArrayOutputStream bos = new ByteArrayOutputStream(1000);
Object val = parameters.get(key); byte[] b = new byte[1000];
String sKey = String.valueOf(key); int n;
Object[] sVals = (val == null ? null while ((n = fis.read(b)) != -1) {
: (val instanceof Object[] ? (Object[]) val : new Object[] { val })); bos.write(b, 0, n);
if (sVals != null && sVals.length > 0) { }
for (Object sVal : sVals) { fis.close();
params.add(sKey + "=" + (sVal == null ? "" bos.close();
: URLEncoder.encode(String.valueOf(sVal), encode == null ? "utf-8" : encode))); add(bos.toByteArray());
} }
} else {
params.add("sKey="); 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() {
int length = 0;
for (byte[] b : ds) {
length += b.length;
}
return length;
}
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 StringUtil.join("&", params); return bytes;
} 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;
...@@ -52,8 +50,6 @@ public class PdsProductsUpdateTask { ...@@ -52,8 +50,6 @@ 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);
...@@ -101,11 +97,10 @@ public class PdsProductsUpdateTask { ...@@ -101,11 +97,10 @@ 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());
list.add(productMap); pdsService.savePdsProduct(transactionId, 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;
...@@ -10,7 +8,6 @@ import org.springframework.beans.factory.annotation.Value; ...@@ -10,7 +8,6 @@ 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;
...@@ -30,28 +27,12 @@ public class PdsService { ...@@ -30,28 +27,12 @@ public class PdsService {
private String authValue; private String authValue;
@Transactional @Transactional
public void savePdsProduct(List<Map<String, Object>> list, List<Object> ids) { public void savePdsProduct(Object transactionId, Map<String, Object> productMap) {
List<String> columns = new ArrayList<String>(); jdbcTemplate.save("pds_article", productMap);
if(list != null && list.size() > 0) { Map<String, String> parameters = new HashMap<String, String>();
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>();
parameters.put("transaction_id", ids);
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);
} }
...@@ -63,7 +44,4 @@ public class PdsService { ...@@ -63,7 +44,4 @@ 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