|
@@ -5,6 +5,10 @@ import java.io.InputStream;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.ConcurrentHashMap;
|
|
|
|
|
|
+import cn.hutool.core.lang.Tuple;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import org.apache.http.Header;
|
|
|
+import org.apache.http.HttpEntity;
|
|
|
import org.apache.http.HttpResponse;
|
|
|
import org.apache.http.client.HttpClient;
|
|
|
import org.apache.http.client.config.RequestConfig;
|
|
@@ -13,6 +17,8 @@ import org.apache.http.client.methods.HttpPost;
|
|
|
import org.apache.http.client.methods.HttpPut;
|
|
|
import org.apache.http.entity.StringEntity;
|
|
|
import org.apache.http.impl.client.HttpClients;
|
|
|
+import org.apache.http.util.EntityUtils;
|
|
|
+import sun.misc.BASE64Encoder;
|
|
|
|
|
|
public class HttpClientUtil {
|
|
|
private static Map<String, HttpClientUtil> instanceMap = new ConcurrentHashMap<String, HttpClientUtil>();
|
|
@@ -154,4 +160,33 @@ public class HttpClientUtil {
|
|
|
String result = baos.toString("UTF-8");
|
|
|
return result;
|
|
|
}
|
|
|
+
|
|
|
+ public synchronized Tuple postImg(String url, String content, Integer timeout, Map<String, String> headerMap) throws Exception {
|
|
|
+ HttpPost httpost = new HttpPost(url);
|
|
|
+ if (headerMap != null) {
|
|
|
+ for (String key : headerMap.keySet()) {
|
|
|
+ httpost.setHeader(key, headerMap.get(key));
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (timeout == null) {
|
|
|
+ timeout = 300000;
|
|
|
+ }
|
|
|
+ if (timeout != null) {
|
|
|
+ RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout)
|
|
|
+ .setSocketTimeout(timeout).build();
|
|
|
+ httpost.setConfig(requestConfig);
|
|
|
+ }
|
|
|
+ StringEntity entity = new StringEntity(content, "UTF-8");
|
|
|
+ httpost.setEntity(entity);
|
|
|
+ entity.setContentType("application/json");
|
|
|
+ HttpResponse httpResponse = client.execute(httpost);
|
|
|
+ String guid= httpResponse.getHeaders("guid")[0].getValue();
|
|
|
+ HttpEntity entity2 = httpResponse.getEntity();
|
|
|
+ byte[] data= EntityUtils.toByteArray(entity2);
|
|
|
+ BASE64Encoder encoder=new BASE64Encoder();
|
|
|
+ String imageBase64="data:image/png;base64,"+encoder.encodeBuffer(data).trim();
|
|
|
+ imageBase64=imageBase64.replaceAll("\n","").replaceAll("\r","").replaceAll(" ","");
|
|
|
+ Tuple tuple = new Tuple(guid, imageBase64);
|
|
|
+ return tuple;
|
|
|
+ }
|
|
|
}
|