需求
再对海康摄像头进行二次开发时,需要将海康提供的.dll(window)文件或.so(linux)文件通过路径的方式进行配置,项目在window上开发.在linux上进行部署,另一方面在linux上是通过jar包部署,无法直接获取到jar中.dll与.so文件的路径,需做成自动获取
问题
1.获取jar包中的指定的文件
2.如何填写有效的文件路径
通过jar包启动
获取jar包的路径,通过JarFile解析jar包,获取到想要的数据.复制到对应的文件夹下,将路径返回
代码
//通过jar包启动
public static String getLibraryPath(String type, String name) {
String fileName = null;
URL url= Application.class.getProtectionDomain().getCodeSource().getLocation();//获取jar包的路径
try{
//转化为utf-8编码,支持中文
String path= URLDecoder.decode(url.getPath(),"utf-8");
if(path.startsWith("file:")) {
//判断是什么系统
if (SystemUtils.name().equals("windows")) {
path = path.replace("file:/", "");
} else if (SystemUtils.name().equals("linux")) {
path = path.replace("file:", "");
}
}
if(path.contains(".jar!/")) {
path = path.substring(0, path.indexOf(".jar!/")+4);//去掉后缀
}
// System.out.println(path);
File file = new File(new File(path).getParentFile() + File.separator + type);//当前jar下新增目录
if (!file.exists()) {
JarFile jarFile = new JarFile(path);
Enumeration<JarEntry> entryes = jarFile.entries();
while (entryes.hasMoreElements()) {
JarEntry entry = entryes.nextElement();
if (entry.getName().startsWith("BOOT-INF/classes/" + type)) {//BOOT-INF/classes/linux开头
String replace = entry.getName().replace("BOOT-INF/classes/", "");
System.out.println(replace);
if (!replace.endsWith("/")){ //去掉只有文件夹
FileCopyUtils.copy(replace);
}
}
}
}
fileName = file.getPath() + File.separator + name;
// System.out.println("fileName=" + fileName);
}catch(Exception e){
e.printStackTrace();
}
return fileName;
}
package cn.daming.box.common.utils;
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class FileCopyUtils {
private static InputStream getResource(String location) throws IOException {
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
InputStream in = resolver.getResource(location).getInputStream();
byte[] byteArray = IOUtils.toByteArray(in);
in.close();
return new ByteArrayInputStream(byteArray);
}
/**
* 获取项目所在文件夹的绝对路径
* @return
*/
private static String getCurrentDirPath() {
URL url = org.springframework.util.FileCopyUtils.class.getProtectionDomain().getCodeSource().getLocation();
String path = url.getPath();
if(path.startsWith("file:")) {
path = path.replace("file:", "");
}
if(path.contains(".jar!/")) {
path = path.substring(0, path.indexOf(".jar!/")+4);
}
File file = new File(path);
path = file.getParentFile().getAbsolutePath();
return path;
}
private static Path getDistFile(String path) throws IOException {
String currentRealPath = getCurrentDirPath();
Path dist = Paths.get(currentRealPath + File.separator + path);
Path parent = dist.getParent();
if(parent != null) {
Files.createDirectories(parent);
}
Files.deleteIfExists(dist);
return dist;
}
/**
* 复制classpath下的文件到jar包的同级目录下
* @param location 相对路径文件,例如kafka/kafka_client_jaas.conf
* @return
* @throws IOException
*/
public static String copy(String location) throws IOException {
InputStream in = getResource("classpath:"+location);
Path dist = getDistFile(location);
Files.copy(in, dist);
in.close();
return dist.toAbsolutePath().toString();
}
}
不通过jar包启动
通过开发工具启动,实现自动识别路径.需要复制到一个固定的地址.
在标准SDK中提供了java.lang.System类,这个类定义了一个对系统设备(包括系统属性和系统输入输出数据流)的、与平台无关的接口。如果我们想获取JVM的系统属性,就需要用到该类的getProperty()方法。该方法可以获取系统(JVM)的配置信息。
String library = java.lang.System.getProperty(“java.library.path”);
String[] librarys = library.split(";");
String libraryP1 = librarys[0]
| Key | Description of Associated Value | 中文描述 |
|---|---|---|
| java.version | Java Runtime Environment version | Java 运行时环境版本 |
| java.vendor | Java Runtime Environment vendor | Java 运行时环境供应商 |
| java.vendor.url | Java vendor URL | Java 供应商的 URL |
| java.home | Java installation directory | Java 安装目录 |
| java.vm.specification.version | Java Virtual Machine specification version | Java 虚拟机规范版本 |
| java.vm.specification.vendor | Java Virtual Machine specification vendor | Java 虚拟机规范供应商 |
| java.vm.specification.name | Java Virtual Machine specification name | Java 虚拟机规范名称 |
| java.vm.version | Java Virtual Machine implementation version Java | 虚拟机实现版本 |
| java.vm.vendor | Java Virtual Machine implementation vendor | Java 虚拟机实现供应商 |
| java.vm.name | Java Virtual Machine implementation name | Java 虚拟机实现名称 |
| java.specification.version | Java Runtime Environment | specification version Java 运行时环境规范版本 |
| java.specification.vendor | Java Runtime Environment | specification vendor Java 运行时环境规范供应商 |
| java.specification.name | Java Runtime Environment | specification name Java 运行时环境规范名称 |
| java.class.version | Java class format version number | Java 类格式版本号 |
| java.class.path | Java class path | Java 类路径 |
| java.library.path | List of paths to search when loading libraries | 加载库时搜索的路径列表 |
| java.io.tmpdir | Default temp file path | 默认的临时文件路径 |
| java.compiler | Name of JIT compiler to use | 要使用的 JIT 编译器的名称 |
| java.ext.dirs | Path of extension directory or directories | 一个或多个扩展目录的路径 |
| os.name | Operating system name | 操作系统的名称 |
| os.arch | Operating system architecture | 操作系统的架构 |
| os.version | Operating system version | 操作系统的版本 |
| file.separator | File separator ("/" on UNIX) | 文件分隔符(在 UNIX 系统中是“/”) |
| path.separator | Path separator (":" on UNIX) | 路径分隔符(在 UNIX 系统中是“:”) |
| line.separator | Line separator ("\n" on UNIX) | 行分隔符(在 UNIX 系统中是“/n”) |
| user.name | User’s account name | 用户的账户名称 |
| user.home | User’s home directory | 用户的主目录 |
| user.dir | User’s current working directory | 用户的当前工作目录 |
代码
public static String getLibraryPath(String type, String name) {
// root path
String library = java.lang.System.getProperty("java.library.path");
String[] librarys = library.split(";");
String temPath = "";
if(librarys != null && librarys.length > 0) {
String libraryP1 = librarys[0];
temPath = libraryP1 + File.separator + type;//系统下的目录文件
// System.out.println("temPath=" + temPath);
if(!new File(temPath).exists()) {
try {
FileUtils.copy("src/main/resources" + File.separator + type,temPath);
} catch (Exception e) {
e.printStackTrace();
}
}
}
return temPath + File.separator + name;
}
修改海康sdk
public static WinHCSadpSDK instance() {
/**
* 以下加载资源文件方法兼容window、linux,同时支持在IDE工具下或者jar环境下使用
*/
String temPath = null;
if (SystemUtils.name().equals("windows")) {
temPath = LibraryUtils.getLibraryPath("window", "Sadp.dll");
} else if (SystemUtils.name().equals("linux")) {
temPath = LibraryUtils.NewgetLibraryPath("linux", "libsadp.so");
}
// 加载LibraryPath下资源文件
WinHCSadpSDK INSTANCE = (WinHCSadpSDK) Native.loadLibrary(temPath, WinHCSadpSDK.class);
return INSTANCE;
}
版权声明:本文为weixin_44939996原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。