如何根據銀行卡號獲取對應的銀行編號,如:ICBC

南北雪樹發表於2018-09-28

最近專案裡面需要使用一個東西,就是如何根據銀行卡號獲取對應的銀行編號,如:ICBC,所以就在網上找了些資源,在此整理分享給大家。都是乾貨哦!

廢話少說,直接上程式碼了。

package com.whb.utils;

import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * @author whb
 * @date 2018年9月28日 下午3:31:12 
 * @Description: 根據銀行卡號獲取對應的銀行變化如:ICBC
 * https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardBinCheck=true&cardNo=6217360599000454941
 * {"cardType":"DC","bank":"ZJNX","key":"6217360599000454941","messages":[],"validated":true,"stat":"ok"}
 */
public class BankCardNoUtil {
	
	private static Logger log = LoggerFactory.getLogger(BankCardNoUtil.class);
	
	private static CloseableHttpClient httpClient = HttpClients.createDefault();

	private static int TIMEOUT = 1800000;

	private static RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(TIMEOUT)
			.setSocketTimeout(TIMEOUT).build();
	
	
	private static String RequestUrl = "https://ccdcapi.alipay.com/validateAndCacheCardInfo.json?_input_charset=utf-8&cardBinCheck=true&cardNo=";
	
	/**
	 * 根據銀行卡號獲取對應的銀行變化如:ICBC
	 * @param bankCardId
	 * @return
	 */
	public static String getBankCardNo(String bankCardId){
		try {
			String result = getString(RequestUrl+bankCardId, "UTF-8");
			if(StringUtils.isNotBlank(result)){
				JSONObject json = JSON.parseObject(result);
				boolean validated = json.getBoolean("validated");
				if(validated){
					return json.getString("bank");
				}
			}
		} catch (Exception e) {
			log.info("獲取銀行卡有誤");
		}
		return null;
	}

	public static void main(String[] args) {
		System.out.println(BankCardNoUtil.getBankCardNo("6217360599000454941"));
	}
	
	
	/**
	 * get方式呼叫http,返回byte[]
	 * @param String url
	 * @return byte[]
	 * @throws Exception
	 */
	private static byte[] getBytes(String url) throws Exception {
		HttpGet request = null;
		try {
			// HTTP請求
			request = new HttpGet(url);
			request.setConfig(requestConfig);
			log.debug(request.getRequestLine().toString());

			// 傳送請求,返回響應
			HttpResponse response = httpClient.execute(request);
			// 響應成功
			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode == HttpStatus.SC_OK) {
				InputStream stream = response.getEntity().getContent();
				return IOUtils.toByteArray(stream);
			}
		} catch (Exception e) {
			throw new Exception("GET請求失敗:[" + url + "]", e);
		} finally {
			if (request != null) {
				try {
					request.releaseConnection();
				} catch (Exception e) {
				}
			}
		}
		return null;
	}

	/**
	 * get方式呼叫http,返回String
	 * 
	 * @param String url
	 * @param String charsetName
	 * @return String
	 * @throws Exception
	 */
	private static String getString(String url, String charsetName) throws Exception {
		byte[] bytes = getBytes(url);
		if (bytes == null || bytes.length <= 0) {
			return "";
		}
		return new String(bytes, charsetName);
	}

}

如果還想其他渠道去查詢的話,這裡有提供相關地址:查詢銀行卡歸屬http://www.guabu.com/bank/

好了,滿滿的愛都給你們了。

相關文章