農行深證通java開發接口連接代碼(jni連接)

最近項目開發需要連接到農行的劃款接口,消息接口是通過深圳通連接的農行,根據深圳通提供的開發接口,c的編譯代碼,開發的java類利用jni調用:

java調用類如下:可以和相關的c代碼(dll或者so文件放在項目根目錄)一起打包成jar包實現解耦和;

package   com.sscc.fdep;
 
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
 
/**
 * 
 * @author huanghe
 * 農行jni 調用c 連接深圳通消息接口
 */
public class mrapi 
{ 
static 
{ 
			//System.loadLibrary("mrapi"); 
	try {
		loadLib("mrapi");
	} catch (IOException e) {		
		e.printStackTrace();
	}
}
 
public static void main(String[] args) 
{ 
 
}  
 
/********************************* FDEP V3 **********************************/
 
/* 初始化,返回爲整形
 * return: 0-failed; 1-OK
   App和AppPwd對應c接口中的AppId和AppPwd, 後四個參數對應C接口中的結構體oConnInfo中各個字段
 */
public native static int MrInit(String App,String AppPwd, String Ip,short Port,String Ipbak, short Portbak);
 
 
 
/* 發送消息 ,參數中SourceAppID填初始化的時候用的App
 * return:    ""-發送失敗   pkgId-發送成功  
   psPkg中是要發送的數據,其餘字段同C接口中pMsgPropery中各個字段 
 */
 
public native static String MrSend( byte[] psPkg, String SourceUserID,String SourceAppID, String DestUserID, String DestAppID,
String PkgID, String CorrPkgID, String UserData1, String UserData2, String ExpiredAbsTime, byte flag, byte Protocltype,int iMillSecTimeo);
 
 
 
 
//return "NULL"-接收失敗    否則返回pkID(64byte) + CorrpkID(64byte) + sourceUserID(64byte) + sourceAppID(64byte) +destUserID(64byte) + UserData1(256byte) + UserData2(256byte) + data     上述前7個字段同C接口中pMsgPropery中各個字段 ,data爲真正的數據 
 
public native static byte[] MrReceive1(String sAppID,String SourceUserID,String SourceAppID, String DestUserID, String DestAppID,
String PkgID, String CorrPkgID, String UserData1, String UserData2, String ExpiredAbsTime, byte flag, byte Protocltype,int iMillSecTimeo);
 
 
 
/* 判斷與交換中樞的連接是否正常
 * return: 0-link_not_ok; 1-link_ok
   參數爲mrinit中設置的app
 */
public native static int MrIsLinkOK(String sAppID);
 
/*釋放資源
  參數爲mrinit中設置的app*/
public native static void MrDestroy(String sAppID);
 
 
/********************************* FDEP V4 **********************************/
 
/* 初始化,返回爲整形
 * return: 0-failed; 1-OK
   App和AppPwd對應c接口中的AppId和AppPwd, 後四個參數對應C接口中的結構體oConnInfo中各個字段
 */
public native static int Mr2Init(String App,String AppPwd, String Ip,short Port,String Ipbak, short Portbak);
 
 
 
/* 發送消息 ,參數中SourceAppID填初始化的時候用的App
 * return:    ""-發送失敗   pkgId-發送成功  
   psPkg中是要發送的數據,其餘字段同C接口中pMsgPropery中各個字段 
 */
 
public native static String Mr2Send( byte[] psPkg, String SourceUserID,String SourceAppID, String DestUserID, String DestAppID,
String PkgID, String CorrPkgID, String UserData1, String UserData2, String MsgType, byte flag, byte BizType, byte Priority, byte SensitiveLevel, int iMillSecTimeo);
 
 
 
 
//return "NULL"-接收失敗    否則返回pkID(64byte) + CorrpkID(64byte) + sourceUserID(64byte) + sourceAppID(64byte) +destUserID(64byte) + UserData1(256byte) + UserData2(256byte) + data     上述前7個字段同C接口中pMsgPropery中各個字段 ,data爲真正的數據 
 
public native static byte[] Mr2Receive1(String sAppID,String SourceUserID,String SourceAppID, String DestUserID, String DestAppID,
String PkgID, String CorrPkgID, String UserData1, String UserData2, int iMillSecTimeo);
 
 
 
/* 判斷與交換中樞的連接是否正常
 * return: 0-link_not_ok; 1-link_ok
   參數爲mrinit中設置的app
 */
public native static int Mr2IsLinkOK(String sAppID);
 
/*釋放資源
  參數爲mrinit中設置的app*/
public native static void Mr2Destroy(String sAppID);
 
 
/* 獲取版本信息
 * return 版本號字符串
 */
public native static byte[]  Mr2GetVersion();
 
 
/* 創建包ID
 * return PkgID
 */
public native static byte[]  Mr2CreatePkgID(String szHandleAppID);
 
/* 獲取對端用戶是否在線狀態
/ return: -1:函數調用失敗  0:不在線    1:在線
*/
public native static int  Mr2GetPeerUserStat(String szHandleAppID,String szPeerUserID);
 
 
/* 調用註冊條件函數
 * return: 0:註冊成功,<0其它失敗
 */
public native static int  Mr2RegRecvCondition(String szHandleAppId,String szSrcUserId, String szSrcAppId, String szDestUserId, String szDestAppId, 
String szPkgId, String szCorrPkgId, String szUserData1,String szUserData2) ;
 
 
///  "NULL,errmsg"-接收失敗   否則返回errcode(4byte)+pkID(64byte) + CorrpkID(64byte) + sourceUserID(32byte) + sourceAppID(32byte) +destUserID(32byte) + destAppID(32byte)+UserData1(256byte) + UserData2(256byte) + data     上述前7個字段同C接口中pMsgPropery中各個字段 ,data爲真正的數據 
///   其中errcode= "0000"爲接收正常,如果errcode爲非"0000"字符串,則由系統返回的錯誤信息,詳細錯誤在data字段中; 
public native static byte[] Mr2Receive3(String sAppID,String SourceUserID,String SourceAppID, String DestUserID, String DestAppID,
String PkgID, String CorrPkgID, String UserData1, String UserData2, int iMillSecTimeo);
//  類初始化調用dll或者so文件
private synchronized  static void loadLib(String libName) throws IOException {   
    String systemType = System.getProperty("os.name");   
    String libExtension = (systemType.toLowerCase().indexOf("win")!=-1) ? ".dll" : ".so";        
    String libFullName = libName + libExtension;   
        
    String nativeTempDir = System.getProperty("java.io.tmpdir");   
        
    InputStream in = null;   
    BufferedInputStream reader = null;   
    FileOutputStream writer = null;   
    
    String[] fileType = new String[8];
	String resource = "/win/";      	
	if(systemType.toLowerCase().indexOf("win")!=-1){    		
		fileType = new String[]{"mr2api.h","mrapi_log.conf","mrapi.dll","mrapi.h","mrapi.lib"};    		    		
	}else{   		
		fileType = new String[]{"mr2api.h","mrapi_log.conf","libmrapi.so","mrapi.h","libinfo.txt","history.txt"};
		resource = "/hp/";
		libFullName = "libmrapi"+libExtension;
	}   
    
    
    File extractedLibFile = new File(nativeTempDir+File.separator+libFullName);  
    File file ;
  //  System.out.println(extractedLibFile.getPath());
    if(!extractedLibFile.exists()){         	  	    	
        try {
        	
        	for(String fname:fileType){
        		 in = mrapi.class.getResourceAsStream(resource + fname);        		 
        		  if(in!=null){   
                 	 reader = new BufferedInputStream(in);   
                 	 file = new File(nativeTempDir+File.separator+fname);
                      writer = new FileOutputStream(file);
                      byte[] buffer = new byte[1024];   
                      
                      while (reader.read(buffer) > 0){   
                          writer.write(buffer);   
                          buffer = new byte[1024];   
                      }
                      if(in!=null)   
                          in.close();   
                      if(writer!=null)   
                          writer.close();
                 }   
        		
        	}                                          
        } catch (IOException e){   
            e.printStackTrace();   
        } finally {   
            if(in!=null)   
                in.close();   
            if(writer!=null)   
                writer.close();   
        }   
    } 
    
    System.load(extractedLibFile.getPath());   
    
	}
}

————————————————
這時如果打成jar包的時候的結構,這樣直接加到maven庫中使用,很方便了:
![在這裏插入圖片描述](https://img-blog.csdnimg.cn/20200403165505977.png)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章