package appletjni;
import java.net.URL;
import java.io.*;
public classtestJNI{
public testJNI() {
}
/*
static {
System.loadLibrary("testDll");//不使用这种加载方式
}*/
public static native int func1(String str, int type);
//从服务器获取动态库文件,保存到客户端
public static voidgetRemoteFile(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 voidloadLibrary(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);
}
}