java applet调用JNI

package appletjni;

import java.net.URL;
import java.io.*;

public class testJNI {

public testJNI() {
}

/*
    static {
        System.loadLibrary("testDll");//不使用这种加载方式
    }*/

public static native int func1(String str, int type);

  //从服务器获取动态库文件,保存到客户端
public static void getRemoteFile(String RemoteFile, String localFile) throws Exception{
    System.out.println("try to get " + RemoteFile + ", save to " + localFile);
    try{
      URL url = new URL(RemoteFile);
      InputStream Stream = url.openConnection().getInputStream();
      byte[] buf = new byte[1024];
      FileOutputStream os = new FileOutputStream(new File(localFile));

      int readcount = 1;
      while (readcount >= 0) {
        readcount = Stream.read(buf);
        if(readcount>0){
          os.write(buf, 0, readcount);
          os.flush();
        }
      }
      os.close();
      Stream.close();
      System.out.println("getRemoteFile done");
    }
    catch(Exception e){
      throw e;
    }
}

static final String localDir = System.getProperty("user.home") + File.separator;//客户端目录

public static void loadLibrary(URL url){//加载动态库
    String dllFileName = "testDll";
    try{
      String OsName = System.getProperty("os.name");
      if(OsName.contains("Windows")){
        dllFileName += ".dll";
      }
      else{
        dllFileName += ".so";
      }
      File file = new File( localDir + dllFileName );
      System.out.println("check file exists: " + localDir + dllFileName);
      if(!file.exists()){
        getRemoteFile(url.toString() + dllFileName, localDir + dllFileName);
      }
      System.out.println("load " + localDir + dllFileName);
      System.load( localDir + dllFileName );
    }
    catch(Exception e){
      System.out.println("can not load " + dllFileName + ", " + e.getMessage());
      e.printStackTrace();
    }
}

}

package appletjni;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.io.*;
import java.security.AccessController;
import java.security.PrivilegedAction;

import org.apache.commons.net.io.CopyStreamListener;
import org.apache.commons.net.ftp.*;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.io.CopyStreamEvent;
import org.apache.commons.net.io.ToNetASCIIOutputStream;
import org.apache.commons.net.io.Util;

public class testApplet1
    extends Applet {

//Construct the applet
public testApplet1() { 
}

//Initialize the applet
public void init() {
    try {
      jbInit();
    }
    catch (Exception e) {
      e.printStackTrace();
    }
}

//Component initialization
private void jbInit() throws Exception {
    System.out.println("getCodeBase: " + this.getCodeBase());

    testJNI.loadLibrary(this.getCodeBase());//加载JNI的动态库

}
//测试
public void test{
    System.out.println("test testJNI.func1");
    int ret = testJNI.func1("aaaaaaaaaa", 123456);
    System.out.println("return " + ret); 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值