方法一:使用 org.apache.commons.httpclient;的HttpClient
public Map<String, Object> officialOcrUpload(String filePath) {
Map<String, Object> reContent = new HashMap<>();
String soapResponseInfo = "";
File targeFile = new File(filePath);
HttpClient httpClient = new HttpClient();
PostMethod postMethod = new PostMethod(OFFICIAL_ENDPOINT+"?bizType=tm");
int state = 500;
try {
FilePart filePart = new FilePart(targeFile.getName(), targeFile);
filePart.setContentType("multipart/form-data");
filePart.setName("file");
Part[] parts = {filePart};
postMethod.setRequestEntity(new MultipartRequestEntity(parts, postMethod.getParams()));
httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(Integer.valueOf(OFFICIAL_TIMEOUT));
state = httpClient.executeMethod(postMethod);
if (state == HttpStatus.SC_OK) {
BufferedReader bufferedReader =