工具包
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import java.io.*;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
public class HttpClientUtil {
public static JSONObject httpPost(String url, String param) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost method = new HttpPost(url);
method.setHeader("Content-Type", "application/json");
JSONObject jsonResult = new JSONObject();
try {
if (StringUtils.isNotBlank(param)) {
StringEntity entity = new StringEntity(param, "UTF-8");
entity.setContentType("application/json");
method.setEntity(entity);
}
HttpResponse result = httpClient.execute(method);
URLDecoder.decode(url, "UTF-8");
int statusCode = result.getStatusLine().getStatusCode();
if (statusCode == 200) {
String str = EntityUtils.toString(result.getEntity());
JSONObject resObj = JSONObject.parseObject(str);
jsonResult.put("success", true);
jsonResult.put("message", "调用成功");
jsonResult.put("data", resObj);
} else if (statusCode == 500) {
jsonResult.put("success", false);
jsonResult.put("message", "服务器内部错误");
} else if (statusCode == 404) {
jsonResult.put("success", false);
jsonResult.put("message", "接口没有找到");
} else {
jsonResult.put("success", false);
jsonResult.put("message", "调用出错");
}
return jsonResult;
} catch (IOException e) {
e.printStackTrace();
} finally {
method.releaseConnection();
try {
httpClient.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return jsonResult;
}
public static String uploadFileByHttpClient(String fileName, File file, String url, String token) {
StringBuilder result = new StringBuilder();
CloseableHttpClient httpClient = HttpClients.createDefault();
CloseableHttpResponse response = null;
InputStream content = null;
BufferedReader in = null;
try {
HttpPost httpPost = new HttpPost(url);
MultipartEntityBuilder builder = MultipartEntityBuilder.create().setMode(HttpMultipartMode.RFC6532);
builder.addBinaryBody("files", file, ContentType.MULTIPART_FORM_DATA, fileName);
HttpEntity entity = builder.build();
httpPost.setEntity(entity);
response = httpClient.execute(httpPost);
HttpEntity responseEntity = response.getEntity();
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
content = responseEntity.getContent();
in = new BufferedReader(new InputStreamReader(content));
String line;
while ((line = in.readLine()) != null) {
result.append(line);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
httpClient.close();
if (content != null) {
content.close();
}
if (in != null) {
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return result.toString();
}
public static JSONObject downloadFile(String url, String param, String pdfFilePath) {
CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost method = new HttpPost(url);
method.setHeader("Content-Type", "application/json");
JSONObject jsonResult = new JSONObject();
try {
if (StringUtils.isNotBlank(param)) {
StringEntity entity = new StringEntity(param, StandardCharsets.UTF_8);
entity.setContentType("application/json");
method.setEntity(entity);
}
HttpResponse result = httpClient.execute(method);
url = URLDecoder.decode(url, "UTF-8");
int statusCode = result.getStatusLine().getStatusCode();
if (statusCode == 200) {
HttpClientUtil.writeToPdf(result.getEntity(), pdfFilePath);
jsonResult.put("success", true);
jsonResult.put("type", "pdf");
jsonResult.put("message", "下载成功");
jsonResult.put("data", pdfFilePath);
return jsonResult;
} else if (statusCode == 500) {
jsonResult.put("success", false);
jsonResult.put("message", "服务器内部错误");
} else if (statusCode == 404) {
jsonResult.put("success", false);
jsonResult.put("message", "接口没有找到");
} else {
jsonResult.put("success", false);
jsonResult.put("message", "调用出错");
}
return jsonResult;
} catch (IOException e) {
} catch (Exception e) {
} finally {
method.releaseConnection();
try {
httpClient.close();
} catch (IOException e) {
}
}
return jsonResult;
}
public static void writeToPdf(HttpEntity entity, String filePath) throws Exception {
BufferedInputStream bis = null;
FileOutputStream fos = null;
BufferedOutputStream bos = null;
try {
byte[] bytes = EntityUtils.toByteArray(entity);
ByteArrayInputStream byteInputStream = new ByteArrayInputStream(bytes);
bis = new BufferedInputStream(byteInputStream);
File file = new File(filePath);
File path = file.getParentFile();
if (!path.exists()) {
path.mkdirs();
}
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
byte[] buffer = new byte[1024];
int length = bis.read(buffer);
while (length != -1) {
bos.write(buffer, 0, length);
length = bis.read(buffer);
}
bos.flush();
} catch (Exception e) {
} finally {
try {
if (bos != null) {
bos.close();
}
if (fos != null) {
fos.close();
}
if (bis != null) {
bis.close();
}
} catch (IOException e) {
}
}
}
}