最近项目开发需要连接到农行的划款接口,消息接口是通过深圳通连接的农行,根据深圳通提供的开发接口,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)