import java.io.FileInputStream;
import java.util.Base64;
import java.util.Base64.Encoder;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
public class bingtopDama {
public static void main(String[] args) {
// 读取图片base64编码数据
String captchaData = bingtopDama.getImageStr("图片路径.jpg");
// post图片数据,账号信息,进行打码
String returnStr = bingtopDama.sendPost(
"http://www.bingtop.com/ocr/upload/",
"username=用户名&password=密码&captchaType=验证码类型&captchaData="+captchaData,"UTF-8"
);
System.out.println(returnStr);
}
public static String getImageStr(String imgFile) {
/**
* @Description: 根据图片地址转换为base64编码字符串
* @return String
*/
FileInputStream inputStream = null;
byte[] data = null;
try {
inputStream = new FileInputStream(imgFile);
data = new byte[inputStream.available()];
inputStream.read(data);
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
Encoder encoder = Base64.getEncoder();
String encodedata = encoder.encodeToString(data);
String encode="";
try {
encode = URLEncoder.encode(encodedata,"UTF-8");
} catch (Exception e) {
System.out.println("转码异常!"+e);
e.printStackTrace();
}
return encode;
}
public static String sendPost(String url, String param,String charset) {
PrintWriter out = null;
BufferedReader in = null;
String result = "";
String line;
StringBuffer sb=new StringBuffer();
try {
URL realUrl = new URL(url);
// 打开和URL之间的连接
URLConnection conn = realUrl.openConnection();
// 设置通用的请求属性 设置请求格式
conn.setRequestProperty("contentType", charset);
conn.setRequestProperty("content-type", "application/x-www-form-urlencoded");
//设置超时时间
conn.setConnectTimeout(60*1000);
conn.setReadTimeout(60*1000);
// 发送POST请求必须设置如下两行
conn.setDoOutput(true);
conn.setDoInput(true);
// 获取URLConnection对象对应的输出流
out = new PrintWriter(new OutputStreamWriter(conn.getOutputStream(),"UTF-8"));
// 发送请求参数
out.print(param);
// flush输出流的缓冲
out.flush();
// 定义BufferedReader输入流来读取URL的响应 设置接收格式
in = new BufferedReader(
new InputStreamReader(conn.getInputStream(),charset));
while ((line = in.readLine()) != null) {
sb.append(line);
}
result=sb.toString();
} catch (Exception e) {
System.out.println("发送 POST请求出现异常!"+e);
e.printStackTrace();
}
//使用finally块来关闭输出流、输入流
finally{
try{
if(out!=null){
out.close();
}
if(in!=null){
in.close();
}
}
catch(IOException ex){
ex.printStackTrace();
}
}
return result;
}
}