Commit b83cdf6a authored by Quxl's avatar Quxl

x

parent 7de2432f
package com.egolm.pds.config;
import java.io.DataOutputStream;
import java.io.UnsupportedEncodingException;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
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 {
......@@ -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;
try {
connection = (HttpURLConnection) new URL(requestUrl).openConnection();
connection.setDoOutput(true);
connection.setDoInput(true);
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());
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.setUseCaches(false);
connection.setInstanceFollowRedirects(true);
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.connect();
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.write(toQueryString(parameters).getBytes());
out.flush();
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);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
} finally {
connection.disconnect();
}
}
public static String toQueryString(Map<?, ?> parameters) {
return toQueryString(parameters, null);
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 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)));
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];
}
} else {
params.add("sKey=");
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);
} catch (UnsupportedEncodingException e) {
throw new HttpUrlEncodingException("URL编码异常", e);
return bytes;
}
}
}
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;
......@@ -52,8 +50,6 @@ 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);
......@@ -101,11 +97,10 @@ public class PdsProductsUpdateTask {
productMap.put("trade_mode", trade_mode);
productMap.put("weight_article", weight_article);
productMap.put("dLastUpdateTime", new Date());
list.add(productMap);
idList.add(transactionId);
pdsService.savePdsProduct(transactionId, productMap);;
}
}
pdsService.savePdsProduct(list, idList);;
return jsonArray == null ? 0 : jsonArray.size();
}
......
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;
......@@ -10,7 +8,6 @@ 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;
......@@ -30,28 +27,12 @@ public class PdsService {
private String authValue;
@Transactional
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>();
parameters.put("transaction_id", ids);
public void savePdsProduct(Object transactionId, Map<String, Object> productMap) {
jdbcTemplate.save("pds_article", productMap);
Map<String, String> parameters = new HashMap<String, String>();
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);
}
......@@ -63,7 +44,4 @@ public class PdsService {
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