|
@@ -2,18 +2,16 @@ package com.dgtly.wxportal.utils.ESign;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.dgtly.common.exception.BusinessException;
|
|
|
-import com.dgtly.common.utils.http.HttpUtils;
|
|
|
import com.dgtly.wxportal.config.ESignConfig;
|
|
|
+import com.dgtly.wxportal.exception.ESignException;
|
|
|
+import org.apache.commons.io.FileUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
import javax.net.ssl.*;
|
|
|
-import java.io.BufferedReader;
|
|
|
-import java.io.IOException;
|
|
|
-import java.io.InputStreamReader;
|
|
|
-import java.io.PrintWriter;
|
|
|
+import java.io.*;
|
|
|
import java.net.ConnectException;
|
|
|
import java.net.SocketTimeoutException;
|
|
|
import java.net.URL;
|
|
@@ -29,6 +27,24 @@ public class ESignHttpUtil {
|
|
|
@Autowired
|
|
|
private ESignConfig eSignConfig;
|
|
|
|
|
|
+ /**
|
|
|
+ * 根据E签宝的大部分返回值实体简单处理结果增加异常判断
|
|
|
+ * @param eurl
|
|
|
+ * @param param
|
|
|
+ * @param querys
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public JSONObject doPostGetJson(ESignUrl eurl, JSONObject param,String ...querys){
|
|
|
+ String url = eurl.getUrl(querys);
|
|
|
+ String str = doPost(url,param.toJSONString());
|
|
|
+ JSONObject json =JSONObject.parseObject(str);
|
|
|
+ int code = json.getInteger("code");
|
|
|
+ if(code!=0){
|
|
|
+ throw new ESignException(code,url,json.getString("message"));
|
|
|
+ }
|
|
|
+ return json;
|
|
|
+ }
|
|
|
+
|
|
|
public String doPost(ESignUrl eurl, JSONObject param,String ...querys){
|
|
|
String url = eurl.getUrl(querys);
|
|
|
return doPost(url,param.toJSONString());
|
|
@@ -36,7 +52,7 @@ public class ESignHttpUtil {
|
|
|
|
|
|
|
|
|
/**
|
|
|
- * 向指定 URL 发送POST方法的请求
|
|
|
+ * 向指定 URL 发送POST方法的请求并添加E签宝所需的请求头
|
|
|
*
|
|
|
* @param url 发送请求的 URL
|
|
|
* @param json 请求参数,
|
|
@@ -84,22 +100,22 @@ public class ESignHttpUtil {
|
|
|
}
|
|
|
catch (ConnectException e)
|
|
|
{
|
|
|
- log.error("调用ESignHttpUtil.sendJsonPost ConnectException, url=" + url + ",param_body=" + json, e);
|
|
|
+ log.error("调用ESignHttpUtil.doPost ConnectException, url=" + url + ",param_body=" + json, e);
|
|
|
throw new BusinessException("调用ESignHttpUtil.sendJsonPost ConnectException, url=" + url + ",param=" + json );
|
|
|
}
|
|
|
catch (SocketTimeoutException e)
|
|
|
{
|
|
|
- log.error("调用ESignHttpUtil.sendJsonPost SocketTimeoutException, url=" + url + ",param=" + json, e);
|
|
|
+ log.error("调用ESignHttpUtil.doPost SocketTimeoutException, url=" + url + ",param=" + json, e);
|
|
|
throw new BusinessException("调用ESignHttpUtil.sendJsonPost SocketTimeoutException, url=" + url + ",param=" + json );
|
|
|
}
|
|
|
catch (IOException e)
|
|
|
{
|
|
|
- log.error("调用ESignHttpUtil.sendJsonPost IOException, url=" + url + ",param=" + json, e);
|
|
|
+ log.error("调用ESignHttpUtil.doPost IOException, url=" + url + ",param=" + json, e);
|
|
|
throw new BusinessException("调用ESignHttpUtil.sendJsonPost IOException, url=" + url + ",param=" + json );
|
|
|
}
|
|
|
catch (Exception e)
|
|
|
{
|
|
|
- log.error("调用HttpsUtil.sendJsonPost Exception, url=" + url + ",param=" + json, e);
|
|
|
+ log.error("调用HttpsUtil.doPost Exception, url=" + url + ",param=" + json, e);
|
|
|
throw new BusinessException("调用ESignHttpUtil.sendJsonPost Exception, url=" + url + ",param=" + json );
|
|
|
}
|
|
|
finally
|
|
@@ -208,4 +224,108 @@ public class ESignHttpUtil {
|
|
|
return true;
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 向E签宝指定 URL 上传文件
|
|
|
+ *
|
|
|
+ * @param url 发送请求的 URL
|
|
|
+ * @param filePath 文件路径
|
|
|
+ * @return 所代表远程资源的响应结果
|
|
|
+ */
|
|
|
+ public String upLoadFile(String url , String filePath ,String contentMD5,String contentType,int fileLength)
|
|
|
+ {
|
|
|
+
|
|
|
+
|
|
|
+ OutputStream out = null;
|
|
|
+ BufferedReader in = null;
|
|
|
+ StringBuilder result = new StringBuilder();
|
|
|
+ try
|
|
|
+ {
|
|
|
+
|
|
|
+ byte[] fileBytes = FileUtils.readFileToByteArray(new File(filePath));
|
|
|
+ log.info("sendPost - {}", url);
|
|
|
+ // 打开和URL之间的连接
|
|
|
+ SSLContext sc = SSLContext.getInstance("SSL");
|
|
|
+ sc.init(null, new TrustManager[] { new TrustAnyTrustManager() }, new SecureRandom());
|
|
|
+
|
|
|
+ //创建连接
|
|
|
+ java.net.URL realUrl = new URL(url);
|
|
|
+ HttpsURLConnection conn = (HttpsURLConnection) realUrl.openConnection();
|
|
|
+ conn.setRequestMethod("PUT");
|
|
|
+ //设置ssl
|
|
|
+ // 从上述SSLContext对象中得到SSLSocketFactory对象
|
|
|
+ SSLSocketFactory ssf = sc.getSocketFactory();
|
|
|
+ conn.setSSLSocketFactory(ssf);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ conn.setRequestProperty("accept", "*/*");
|
|
|
+ conn.setRequestProperty("connection", "Keep-Alive");
|
|
|
+ //设置请求头
|
|
|
+ conn.setRequestProperty("Content-Type",contentType);
|
|
|
+ conn.setRequestProperty("Content-MD5",contentMD5);
|
|
|
+ conn.setRequestProperty("Content-Length",fileLength+"");
|
|
|
+
|
|
|
+ conn.setUseCaches(false);//设置不要缓存
|
|
|
+ conn.setDoOutput(true);
|
|
|
+ conn.setDoInput(true);
|
|
|
+
|
|
|
+ //写入请求体
|
|
|
+ out = conn.getOutputStream();
|
|
|
+ out.write(fileBytes);
|
|
|
+ out.flush();
|
|
|
+ in = new BufferedReader( new InputStreamReader(conn.getInputStream()));
|
|
|
+ String line;
|
|
|
+ while ((line = in.readLine()) != null)
|
|
|
+ {
|
|
|
+ result.append(line);
|
|
|
+ }
|
|
|
+ log.info("recv - {}", result);
|
|
|
+ }
|
|
|
+ catch (ConnectException e)
|
|
|
+ {
|
|
|
+ log.error("调用ESignHttpUtil.upLoadFile ConnectException, url=" + url + ",filepath=" + filePath, e);
|
|
|
+ throw new BusinessException("调用ESignHttpUtil.upLoadFile ConnectException, url=" + url);
|
|
|
+ }
|
|
|
+ catch (SocketTimeoutException e)
|
|
|
+ {
|
|
|
+ log.error("调用ESignHttpUtil.upLoadFile ConnectException, url=" + url + ",filepath=" + filePath, e);
|
|
|
+ throw new BusinessException("调用ESignHttpUtil.upLoadFile SocketTimeoutException, url=" + url );
|
|
|
+ }
|
|
|
+ catch (IOException e)
|
|
|
+ {
|
|
|
+ log.error("调用ESignHttpUtil.upLoadFile ConnectException, url=" + url + ",filepath=" + filePath, e);
|
|
|
+ throw new BusinessException("调用ESignHttpUtil.upLoadFile IOException, url=" + url );
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ log.error("调用ESignHttpUtil.upLoadFile ConnectException, url=" + url + ",filepath=" + filePath, e);
|
|
|
+ throw new BusinessException("调用ESignHttpUtil.upLoadFile Exception, url=" + url );
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (out != null)
|
|
|
+ {
|
|
|
+ out.close();
|
|
|
+ }
|
|
|
+ if (in != null)
|
|
|
+ {
|
|
|
+ in.close();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (IOException ex)
|
|
|
+ {
|
|
|
+ log.error("调用in.close Exception, url=" + url, ex);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return result.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
}
|