Commit 4cee6888 authored by Quxl's avatar Quxl

x

parent 1d9e20f7
...@@ -77,11 +77,6 @@ public class HttpUtil { ...@@ -77,11 +77,6 @@ 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, 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 {
Data data = new Data(); Data data = new Data();
...@@ -95,26 +90,9 @@ public class HttpUtil { ...@@ -95,26 +90,9 @@ public class HttpUtil {
ParamBuffer.append(parameters.get(key) + "\r\n"); ParamBuffer.append(parameters.get(key) + "\r\n");
} }
} }
ParamBuffer.append("--" + BOUNDARY ParamBuffer.append("--" + BOUNDARY + "\r\nContent-Disposition: form-data; name=\"----------------------------------\"\r\n\r\n-------------------------\r\n");
+ "\r\nContent-Disposition: form-data; name=\"----------------------------------\"\r\n\r\n-------------------------\r\n");
String ParamBufferString = ParamBuffer.toString(); String ParamBufferString = ParamBuffer.toString();
data.add(ParamBufferString.getBytes()); 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"); StringBuffer EndBuffer = new StringBuffer("\r\n--" + BOUNDARY + "--\r\n");
String EndBufferString = EndBuffer.toString(); String EndBufferString = EndBuffer.toString();
data.add(EndBufferString.getBytes()); data.add(EndBufferString.getBytes());
...@@ -122,7 +100,7 @@ public class HttpUtil { ...@@ -122,7 +100,7 @@ public class HttpUtil {
connection = (HttpURLConnection) url.openConnection(); 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", "multipart/form-data; boundary=" + BOUNDARY); connection.setRequestProperty("Content-Type", "application/json; boundary=" + BOUNDARY);
connection.setRequestProperty("Content-Length", String.valueOf(data.length())); connection.setRequestProperty("Content-Length", String.valueOf(data.length()));
if (headers != null) { if (headers != null) {
for (String key : headers.keySet()) { for (String key : headers.keySet()) {
...@@ -137,10 +115,6 @@ public class HttpUtil { ...@@ -137,10 +115,6 @@ public class HttpUtil {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
throw new RuntimeException(e); throw new RuntimeException(e);
} finally {
if(connection != null) {
connection.disconnect();
}
} }
} }
......
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