-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e109037
commit f24495b
Showing
4 changed files
with
102 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...va/me/chanjar/weixin/mp/util/requestexecuter/ocr/OcrDiscernApacheHttpRequestExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package me.chanjar.weixin.mp.util.requestexecuter.ocr; | ||
|
||
import me.chanjar.weixin.common.error.WxError; | ||
import me.chanjar.weixin.common.error.WxErrorException; | ||
import me.chanjar.weixin.common.util.http.RequestHttp; | ||
import me.chanjar.weixin.common.util.http.apache.Utf8ResponseHandler; | ||
import org.apache.http.HttpEntity; | ||
import org.apache.http.HttpHost; | ||
import org.apache.http.client.config.RequestConfig; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpPost; | ||
import org.apache.http.entity.mime.HttpMultipartMode; | ||
import org.apache.http.entity.mime.MultipartEntityBuilder; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
/** | ||
* @author : zhayueran | ||
* @Date: 2019/6/27 14:06 | ||
* @Description: | ||
*/ | ||
public class OcrDiscernApacheHttpRequestExecutor extends OcrDiscernRequestExecutor<CloseableHttpClient, HttpHost> { | ||
|
||
|
||
public OcrDiscernApacheHttpRequestExecutor(RequestHttp requestHttp) { | ||
super(requestHttp); | ||
} | ||
|
||
@Override | ||
public String execute(String uri, File file) throws WxErrorException, IOException { | ||
HttpPost httpPost = new HttpPost(uri); | ||
if (requestHttp.getRequestHttpProxy() != null) { | ||
RequestConfig config = RequestConfig.custom().setProxy(requestHttp.getRequestHttpProxy()).build(); | ||
httpPost.setConfig(config); | ||
} | ||
if (file != null) { | ||
HttpEntity entity = MultipartEntityBuilder | ||
.create() | ||
.addBinaryBody("file", file) | ||
.setMode(HttpMultipartMode.RFC6532) | ||
.build(); | ||
httpPost.setEntity(entity); | ||
} | ||
try (CloseableHttpResponse response = requestHttp.getRequestHttpClient().execute(httpPost)) { | ||
String responseContent = Utf8ResponseHandler.INSTANCE.handleResponse(response); | ||
WxError error = WxError.fromJson(responseContent); | ||
if (error.getErrorCode() != 0) { | ||
throw new WxErrorException(error); | ||
} | ||
return responseContent; | ||
} finally { | ||
httpPost.releaseConnection(); | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...rc/main/java/me/chanjar/weixin/mp/util/requestexecuter/ocr/OcrDiscernRequestExecutor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package me.chanjar.weixin.mp.util.requestexecuter.ocr; | ||
|
||
import me.chanjar.weixin.common.error.WxErrorException; | ||
import me.chanjar.weixin.common.util.http.RequestExecutor; | ||
import me.chanjar.weixin.common.util.http.RequestHttp; | ||
import me.chanjar.weixin.common.util.http.ResponseHandler; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
|
||
/** | ||
* @author : zhayueran | ||
* @Date: 2019/6/27 15:06 | ||
* @Description: | ||
*/ | ||
public abstract class OcrDiscernRequestExecutor<H, P> implements RequestExecutor<String, File> { | ||
protected RequestHttp<H, P> requestHttp; | ||
|
||
public OcrDiscernRequestExecutor(RequestHttp requestHttp) { | ||
this.requestHttp = requestHttp; | ||
} | ||
|
||
@Override | ||
public void execute(String uri, File data, ResponseHandler<String> handler) throws WxErrorException, IOException { | ||
handler.handle(this.execute(uri, data)); | ||
} | ||
|
||
public static RequestExecutor<String, File> create(RequestHttp requestHttp) { | ||
switch (requestHttp.getRequestType()) { | ||
case APACHE_HTTP: | ||
return new OcrDiscernApacheHttpRequestExecutor(requestHttp); | ||
default: | ||
return null; | ||
} | ||
} | ||
} |