JNIApp.java 2.49 KB
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);
                }
            }
        }
    }
}