依据银行卡号获取银行信息Java代码

package com.epay.web;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

/**
 * 根据银行卡Code获得银行卡信息测试类[该类仅仅是测试类、欢迎指正]
 * 参考网址: <a href="http://www.oschina.net/code/snippet_862068_45025">http://www.oschina.net/code/snippet_862068_45025</a>
 * 参考网址:<a href="http://www.atool.org/bank.php">http://www.atool.org/bank.php</a>
 * 非常感谢:<a href="http://my.oschina.net/emaster">e代宗师 </a>
 * 如有资源信息更新请及时联系zhangyq谢谢
 * <P>Author : zhangyq </P>
 * <P>Email : [email protected] </P> 
 * <P>Date : 2015年6月17日 </P>
 */
public class TestBankInfo {

	public static void main(String[] args) throws IOException {
		//------ 注意资源库文件要放到classpath 路径下
		InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("bankList.php");
		Map<String, String> result = readFile(is, null,null);
		if (result != null && !result.isEmpty()) {
			Set<Map.Entry<String, String>> sets = result.entrySet();
			int i = 1;
			
			System.out.println("共有多少条数据记录:"+result.size());
			System.out.println("顺序\tCode\t\t Result\t\t");
			for (Map.Entry<String, String> entity : sets) {
				System.out.println(i+" "+entity.getKey()+" "+entity.getValue());
				i++;
			}
			
		}else{
			System.out.println("暂无记录!");
		}
		//------- 开始调用接口了 
		System.out.println("=============================================");
		System.out.println(getBankInfo(result, "427018", "427019".length()));

	}
	
	/**
	 * 该方法在生产环境可以开放出来
	 * <P>Author :  zhangyq </P>  
	 * <P>Date : 2015年6月17日 </P>
	 * @param map					库资源集合
	 * @param bankCode				用户输入的银行卡卡号	
	 * @param bankCodeLength		用户输入的银行卡卡号长度({3,9})
	 * @return  华融湘江银行(65705500)-华融湘江银行华融公务卡普卡-贷记卡
	 */
	public  static String getBankInfo(Map<String, String> map ,String bankCode,int bankCodeLength){
		String result = "";
		if (bankCodeLength >=3 ) {
			result = map.get(bankCode);
		}else{
			result = "请输入长度大于等于3的银行卡卡号";
		}
		return result;
	}
	/**
	 * Desc: 文件内容定义
	 * 		'622906' => '湖南农村信用社联合社(65385500)-福祥贷记卡-贷记卡',
	 * 		'628392' => '江西省农村信用社联合社(65394200)-百福公务卡-贷记卡',
			'623092' => '江西省农村信用社联合社(65394200)-借记IC卡-借记卡',
			'621778' => '安徽省农村信用社(65473600)-金农卡-借记卡',
			'620528' => '邢台银行(65541310)-金牛市民卡-借记卡',
			'621748' => '商丘市商业银行(65675060)-百汇卡-借记卡',
			'628271' => '商丘市商业银行(65675061)-公务卡-贷记卡',
			'628328' => '华融湘江银行(65705500)-华融湘江银行华融公务卡普卡-贷记卡',
			
		Func: 会去除空格	
	 * <P>Author :  zhangyq </P>  
	 * <P>Date : 2015年6月17日 </P>
	 * @param is				文件输入流
	 * @param charSet			文件字符编码(可以为空)
	 * @param lineSplitRegex 	每一行key、value的分隔符号默认为"=>"
	 * @return	 {key:625970,value: 浦发银行信用卡中心(63100000)-贷记卡-贷记卡}
	 */
	public static Map<String, String> readFile(InputStream is,Charset charSet,String lineSplitRegex){
		Map<String, String> result = new HashMap<String, String>();
		if (charSet == null) {
			charSet = Charset.forName( System.getProperty("file.encoding") );//获取java环境默认编码
		}
		if (lineSplitRegex == null) {
			lineSplitRegex = "=>";
		}
		
		BufferedReader br = new BufferedReader(new InputStreamReader(is,charSet));
		String lineStr = null;
		do {
			try {
				lineStr = br.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			String[] arrays = null;
			if (lineStr != null && lineStr.length() >0 && lineStr.trim() != null ) {
				arrays = lineStr.split(lineSplitRegex);
				if (arrays != null && arrays.length == 2) {
					String key   = arrays[0].replaceAll("'", "").trim();
					String value = arrays[1].replace(",", "").replaceAll("'", "").replaceFirst(" ", "");
					result.put(key, value);
				}
			}
		} while (lineStr != null && lineStr.length() > 0);
		
		return result;
		
	}

}

 资源库文件:见附件

猜你喜欢

转载自javatozhang.iteye.com/blog/2220258