All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.soento.core.util.HttpUtil Maven / Gradle / Ivy
package com.soento.core.util;
import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Map;
/**
* http 工具类
*
* @author soento
*/
public class HttpUtil {
private static final Logger log = LoggerFactory.getLogger(HttpUtil.class);
private static CloseableHttpClient getHttpClient() {
try {
return SpringUtil.getBean(CloseableHttpClient.class);
} catch (Throwable e) {
return HttpClientBuilder.create().build();
}
}
private static RequestConfig getConfig() {
try {
return SpringUtil.getBean(RequestConfig.class);
} catch (Throwable e) {
return RequestConfig.custom().build();
}
}
public static String get(String url) {
try {
log.info("↓↓↓↓↓↓ GET ↓↓↓↓↓↓");
log.info("URL : {}", url);
CloseableHttpClient httpClient = getHttpClient();
RequestConfig config = getConfig();
HttpGet get = new HttpGet(url);
get.setConfig(config);
CloseableHttpResponse response = httpClient.execute(get);
String result = EntityUtils.toString(response.getEntity());
log.info("RESULT : {}", result);
log.info("↑↑↑↑↑↑ GET ↑↑↑↑↑↑");
return result;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String convertParams(Map params) {
StringBuilder result = new StringBuilder();
for (String key : params.keySet()) {
result.append(key);
result.append("=");
result.append(params.get(key));
result.append("&");
}
return result.toString();
}
public static String post(String url, ContentType contentType, String params) {
try {
log.info("↓↓↓↓↓↓ POST ↓↓↓↓↓↓");
log.info("URL : {}", url);
log.info("PARAMS : {}", params);
CloseableHttpClient httpClient = getHttpClient();
RequestConfig config = getConfig();
HttpPost post = new HttpPost(url);
post.setConfig(config);
ByteArrayEntity entity = new ByteArrayEntity(params.getBytes(StandardCharsets.UTF_8), contentType);
post.setEntity(entity);
CloseableHttpResponse response = httpClient.execute(post);
String result = EntityUtils.toString(response.getEntity());
log.info("RESULT : {}", result);
log.info("↑↑↑↑↑↑ POST ↑↑↑↑↑↑");
return result;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static String post(String url, String params) {
return post(url, ContentType.create("application/x-www-form-urlencoded", StandardCharsets.UTF_8), params);
}
public static String postJson(String url, String params) {
return post(url, ContentType.create("application/json", StandardCharsets.UTF_8), params);
}
public static String upload(String url, File[] files, String params) {
try {
log.info("↓↓↓↓↓↓ UPLOAD ↓↓↓↓↓↓");
log.info("URL : {}", url);
CloseableHttpClient httpClient = getHttpClient();
RequestConfig config = getConfig();
HttpPost post = new HttpPost(url);
post.setConfig(config);
MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
if (files != null && files.length > 0) {
for (int i = 0; i < files.length; i++) {
log.info("File Path : {}", files[i].getPath());
entityBuilder.addBinaryBody("file" + i, files[i],
ContentType.create("application/octet-stream", StandardCharsets.UTF_8),
files[i].getName());
}
}
if (StringUtil.isNotBlank(params)) {
String[] paramKV = params.split("&");
for (String kv : paramKV) {
String[] tmp = kv.split("=");
if (tmp.length == 2) {
String k = tmp[0];
String v = tmp[1];
log.info("Param {} : {}", k, v);
entityBuilder.addTextBody(k, v, ContentType.create("text/plain", StandardCharsets.UTF_8));
}
}
}
post.setEntity(entityBuilder.build());
CloseableHttpResponse response = httpClient.execute(post);
String result = EntityUtils.toString(response.getEntity());
log.info("RESULT : {}", result);
log.info("↑↑↑↑↑↑ UPLOAD ↑↑↑↑↑↑");
return result;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static File download(String url, String path) {
File file;
BufferedInputStream in = null;
OutputStream out = null;
try {
log.info("↓↓↓↓↓↓ DOWNLOAD ↓↓↓↓↓↓");
log.info("URL : {}", url);
CloseableHttpClient httpClient = getHttpClient();
RequestConfig config = getConfig();
HttpGet get = new HttpGet(url);
get.setConfig(config);
CloseableHttpResponse response = httpClient.execute(get);
HttpEntity entity = response.getEntity();
in = new BufferedInputStream(entity.getContent());
file = new File(path);
if (!file.getParentFile().exists()) {
file.getParentFile().mkdirs();
}
out = new FileOutputStream(file);
int size = 0;
byte[] buf = new byte[1024];
while ((size = in.read(buf)) != -1) {
out.write(buf, 0, size);
}
out.flush();
log.info("FILE : {}", file.getPath());
log.info("↑↑↑↑↑↑ DOWNLOAD ↑↑↑↑↑↑");
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
StreamUtil.close(out);
StreamUtil.close(in);
}
return file;
}
}