Commit 1301d343 authored by 张永's avatar 张永

增加方法

parent 86965ca6
package com.egolm.common;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
......@@ -215,4 +218,111 @@ public class HttpUtil {
return StringUtil.join("&", params);
}
public static String post(String requestUrl, Map<String, Object> parameters, Map<String, String> headers, Map<String, File> attachments) {
HttpURLConnection connection = null;
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();
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);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
connection.disconnect();
}
}
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 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;
}
}
}
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