sdk设置全局代理

package ocr;

import java.io.IOException;
import java.lang.reflect.Modifier;
import java.net.Authenticator;
import java.net.PasswordAuthentication;
import java.net.URLEncoder;
import java.util.HashMap;

import org.json.JSONObject;

import com.baidu.aip.ocr.AipOcr;
import com.baidu.aip.util.Base64Util;

public class OcrTest
{
	public static final String APP_ID = "C123456";
	public static final String APP_KEY = "";
	public static final String SECRET_KEY = "";
	public static void main(String[] args)
	{
		System.setProperty("proxySet", "true"); 
		System.setProperty("http.proxyHost", "10.244"); 
		System.setProperty("http.proxyPort", "3128"); 
		System.setProperty("https.proxyHost", "10.244"); 
		System.setProperty("https.proxyPort", "3128"); 
        BasicAuthenticator auth = new BasicAuthenticator("账号", "密码");
        Authenticator.setDefault(auth);
        System.out.print(System.getProperty("os.name"));
		
        
		String filePath = "F:\\Workspace\\springboot\\src\\test\\java\\ocr\\888.jpg";
		String access_token = "";
		String otherHost = "https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic";
//		String otherHost = "https://aip.baidubce.com/rest/2.0/ocr/v1/idcard";
		try
		{
			byte[] imgData = FileUtil.readFileByBytes(filePath);
			String imgStr = Base64Util.encode(imgData);
			String prams = URLEncoder.encode("image","UTF-8")+"="+URLEncoder.encode(imgStr,"UTF-8");
			String result = HttpUtil.post(otherHost, access_token, prams);
			System.out.println("识别文字:"+result);
		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	public static String getAuth(String ak,String sk){
		String authHost = "https://aip.baidubce.com/oauth/2.0/token?";
		String mustParam = "grant_type=client_credentials";
		
		return authHost+mustParam+"&client_id="+ak+"&client_secret="+sk;
	}
}

class BasicAuthenticator extends Authenticator {
    String userName;
    String password;
    public BasicAuthenticator(String userName, String password) {
        this.userName = userName;
        this.password = password;
    }

//    @Override
    protected PasswordAuthentication getPasswordAuthentication() {
        return new PasswordAuthentication(userName, password.toCharArray());
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值