包含GET请求方法、POST请求方法、模拟前端文件上传方法
模拟前端文件上传关联的文档
package com.drsanjun.utils;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
public class HttpUtils {
public static String post(String url, String content, String contentType, String accept) {
return streamToString(postForStream(url, content, contentType, accept));
}
public static String get(String url, String contentType, String accept, Map<String, Object> params) {
return streamToString(getForStream(url, contentType, accept, params));
}
public static InputStream postForStream(String url, String content, String contentType, String accept) {
try {
URL postUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Charset", "UTF-8");
connection.setUseCaches(false);
if (contentType != null && !"".equals(contentType)) {
connection.setRequestProperty("Content-Type", contentType);
}
if (accept != null && !"".equals(accept)) {
connection.setRequestProperty("Accept", accept);
}
connection.connect();
DataOutputStream out = new DataOutputStream(connection.getOutputStream());
out.writeBytes(content);
out.flush();
out.close();
return connection.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static InputStream getForStream(String url, String contentType, String accept, Map<String, Object> params) {
try {
StringBuffer parmaSb = new StringBuffer();
if (params != null && !params.isEmpty()) {
Set<Entry<String, Object>> set = params.entrySet();
for (Entry<String, Object> entry : set) {
parmaSb.append(entry.getKey() + "=" + entry.getValue() + "&");
}
url = url + "?" + parmaSb.substring(0, parmaSb.length() - 1);
}
URL postUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) postUrl.openConnection();
connection.setRequestMethod("GET");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Charset", "UTF-8");
if (contentType != null && !"".equals(contentType)) {
connection.setRequestProperty("Content-Type", contentType);
}
if (accept != null && !"".equals(accept)) {
connection.setRequestProperty("Accept", accept);
}
connection.connect();
return connection.getInputStream();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String singleFileUploadWithParameters(String actionURL, String name, InputStream fileStream,
String fileName, String fileType, HashMap<String, String> parameters) {
String end = "\r\n";
String twoHyphens = "--";
String boundary = "----WebKitFormBoundary851PD6JXXxfIPFk9";
String response = "";
try {
URL url = new URL(actionURL);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("Charset", "UTF-8");
connection.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
DataOutputStream ds = new DataOutputStream(connection.getOutputStream());
if (parameters != null) {
Set<String> keys = parameters.keySet();
for (String key : keys) {
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; name=\"");
ds.write(key.getBytes());
ds.writeBytes("\"" + end);
ds.writeBytes(end);
ds.write(parameters.get(key).getBytes());
ds.writeBytes(end);
}
}
ds.writeBytes(twoHyphens + boundary + end);
ds.writeBytes("Content-Disposition: form-data; " + "name=\"" + name + "\"; " + "filename=\"");
ds.write(fileName.getBytes());
ds.writeBytes("\"" + end);
ds.writeBytes("Content-Type: " + fileType + end);
ds.writeBytes(end);
byte[] buffer = new byte[1024];
int length = -1;
while ((length = fileStream.read(buffer)) != -1) {
ds.write(buffer, 0, length);
}
ds.writeBytes(end);
fileStream.close();
ds.writeBytes(twoHyphens + boundary + twoHyphens + end);
ds.writeBytes(end);
ds.flush();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "utf-8"));
String s = "";
String temp = "";
while ((temp = reader.readLine()) != null) {
s += temp;
}
response = s;
reader.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("No response get!!!");
}
ds.close();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Request failed!");
}
return response;
}
public static void disconnect(HttpURLConnection connection) {
connection.disconnect();
connection = null;
}
private static String streamToString(InputStream inStrm) {
try {
StringBuffer buffer = new StringBuffer();
byte[] b = new byte[1024];
int length = -1;
while ((length = inStrm.read(b)) != -1) {
buffer.append(new String(b, 0, length));
}
return buffer.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}