JNIApp.java
2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package com.viontech.keliu;
import lombok.extern.slf4j.Slf4j;
import org.apache.tomcat.util.http.fileupload.util.Streams;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* .
*
* @author 谢明辉
* @date 2021/3/22
*/
@SpringBootApplication
@Slf4j
public class JNIApp implements CommandLineRunner {
public static void main(String[] args) {
try {
SpringApplication.run(JNIApp.class, args);
} catch (Exception e) {
log.error("", e);
}
}
@Override
public void run(String... args) throws IOException {
log.info("系统:{}", System.getProperty("os.name"));
Resource[] resources = null;
if (System.getProperty("os.name").toLowerCase().contains("linux")) {
resources = new PathMatchingResourcePatternResolver().getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + "lib/so/*");
} else if (System.getProperty("os.name").toLowerCase().contains("windows")) {
resources = new PathMatchingResourcePatternResolver().getResources(ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + "lib/dll/*");
}
if (resources != null) {
for (Resource resource : resources) {
try {
String filename = resource.getFilename();
if (filename == null) {
continue;
}
String prefix = filename.substring(0, filename.lastIndexOf(".") - 1);
String suffix = filename.substring(filename.lastIndexOf("."));
File tempFile = File.createTempFile(prefix, suffix);
tempFile.deleteOnExit();
try (InputStream inputStream = resource.getInputStream(); FileOutputStream fileOutputStream = new FileOutputStream(tempFile)) {
Streams.copy(inputStream, fileOutputStream, true);
}
System.load(tempFile.getAbsolutePath());
} catch (Exception e) {
log.error("", e);
}
}
}
}
}