Commit 6adf7707 authored by Quxl's avatar Quxl

x

parent 84afef48
package com.egolm.pds.config; package com.egolm.pds.config;
import java.io.ByteArrayOutputStream; import java.io.PrintWriter;
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,69 +77,27 @@ public class HttpUtil { ...@@ -76,69 +77,27 @@ 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) {
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;
PrintWriter out;
try { try {
Data data = new Data(); URL getUrl = new URL(requestUrl);
String BOUNDARY = "----WebKitFormBoundaryT1HoybnYeFOGFlBR"; connection = (HttpURLConnection) getUrl.openConnection();
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.setRequestMethod("POST");
connection.setRequestProperty("Accept-Charset", "utf-8"); connection.setRequestProperty("Accept-Charset", "utf-8");
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
if(attachments != null && attachments.size() > 0) {
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + BOUNDARY);
} else {
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; boundary=" + BOUNDARY);
}
connection.setRequestProperty("Content-Length", String.valueOf(data.length()));
if (headers != null) { if (headers != null) {
for (String key : headers.keySet()) { for (String key : headers.keySet()) {
connection.setRequestProperty(key, headers.get(key)); connection.setRequestProperty(key, headers.get(key));
} }
} }
connection.setDoOutput(true); connection.setDoOutput(true);
OutputStream out = connection.getOutputStream(); connection.setDoInput(true);
out.write(data.bytes()); out = new PrintWriter(connection.getOutputStream());
out.close(); String queryString = toQueryString(parameters);
out.print(queryString);
out.flush();
connection.connect();
return responseBody(connection); return responseBody(connection);
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); throw new RuntimeException(e);
...@@ -146,51 +105,33 @@ public class HttpUtil { ...@@ -146,51 +105,33 @@ public class HttpUtil {
connection.disconnect(); connection.disconnect();
} }
} }
public static String toQueryString(Map<?, ?> parameters) {
return toQueryString(parameters, null);
}
public static class Data { public static String toQueryString(Map<?, ?> parameters, String encode) throws HttpUrlEncodingException {
try {
private byte[][] ds = new byte[0][0]; List<String> params = new ArrayList<String>();
if (parameters != null) {
public void add(File file) throws IOException { for (Object key : parameters.keySet()) {
FileInputStream fis = new FileInputStream(file); Object val = parameters.get(key);
ByteArrayOutputStream bos = new ByteArrayOutputStream(1000); String sKey = String.valueOf(key);
byte[] b = new byte[1000]; Object[] sVals = (val == null ? null
int n; : (val instanceof Object[] ? (Object[]) val : new Object[] { val }));
while ((n = fis.read(b)) != -1) { if (sVals != null && sVals.length > 0) {
bos.write(b, 0, n); for (Object sVal : sVals) {
} params.add(sKey + "=" + (sVal == null ? ""
fis.close(); : URLEncoder.encode(String.valueOf(sVal), encode == null ? "utf-8" : encode)));
bos.close(); }
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 bytes; return StringUtil.join("&", params);
} catch (UnsupportedEncodingException e) {
throw new HttpUrlEncodingException("URL编码异常", e);
} }
} }
} }
...@@ -29,7 +29,7 @@ public class PdsService { ...@@ -29,7 +29,7 @@ public class PdsService {
@Transactional @Transactional
public void savePdsProduct(Object transactionId, Map<String, Object> productMap) { public void savePdsProduct(Object transactionId, Map<String, Object> productMap) {
jdbcTemplate.save("pds_article", productMap); jdbcTemplate.save("pds_article", productMap);
Map<String, String> parameters = new HashMap<String, String>(); 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()); parameters.put("transaction_id", transactionId == null ? null : transactionId.toString());
......
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