Commit 6adf7707 authored by Quxl's avatar Quxl

x

parent 84afef48
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.PrintWriter;
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,69 +77,27 @@ public class HttpUtil {
}
}
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) {
public static String post(String requestUrl, Map<String, Object> parameters, Map<String, String> headers) {
HttpURLConnection connection = null;
PrintWriter out;
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());
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();
URL getUrl = new URL(requestUrl);
connection = (HttpURLConnection) getUrl.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Accept-Charset", "utf-8");
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()));
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
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();
connection.setDoInput(true);
out = new PrintWriter(connection.getOutputStream());
String queryString = toQueryString(parameters);
out.print(queryString);
out.flush();
connection.connect();
return responseBody(connection);
} catch (Exception e) {
throw new RuntimeException(e);
......@@ -147,50 +106,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);
}
}
}
......@@ -29,7 +29,7 @@ public class PdsService {
@Transactional
public void savePdsProduct(Object transactionId, Map<String, Object> 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>();
headers.put(authKey, authValue);
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