* 解决:当项目打包成jar之后resources路径下面的证书文件访问不到
* 思路:
* 1、运行时先复制一个jar
* 2、将复制的jar解压到jar文件目录
* 3、删除复制的jar跟解压的非证书文件夹
package blockchaincode; import blockchaincode.utils.CryptoUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import blockchaincode.utils.TempCryptoFolderUtil; import com.sunsheen.jfids.das.core.DasApplication; import com.sunsheen.jfids.das.core.annotation.DasBootApplication; /** * * 当独立开发HKDAS应用时(Java工程、Maven工程),使用这种方式启动应用。 * @author WangSong * */ @DasBootApplication() public class DasApplicationBootstrap { private static Logger log = LoggerFactory.getLogger(DasApplicationBootstrap.class); public static void main(String[] args) { //将证书文件拷贝到项目同级目录下 try { CryptoUtil.pass(); } catch (Exception e) { e.printStackTrace(); log.error("当前jar包目录の证书文件拷贝异常!", e); System.err.println("证书文件拷贝异常!"); } //启动 DasApplication.run(DasApplicationBootstrap.class, args); } }
package blockchaincode.utils; import java.io.File; import java.net.URL; import org.apache.derby.impl.tools.sysinfo.Main; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * 解决:当项目打包成jar之后resources路径下面的证书文件访问不到 * 思路: * 1、运行时先复制一个jar * 2、将复制的jar解压到jar文件目录 * 3、删除复制的jar跟解压的非证书文件夹 * @author WangSong */ public class CryptoUtil { private static Logger log = LoggerFactory.getLogger(CryptoUtil.class); private CryptoUtil(){} public static void pass()throws Exception{ /** 获取当前jar位置 **/ URL url = Main.class.getClassLoader().getResource("crypto-config/"); //当前需要是个jar文件 String protocol = url.getProtocol();//大概是jar if (!"jar".equalsIgnoreCase(protocol)) { log.error("没有以jar运行,不重复生成证书文件。"); return; } // jar:file:/home/hkdas-123/HKDAS/app/blockchaincode-provider-1.0.0-jar-with-dependencies.jar!/ String jarPath = url.toString().substring(0, url.toString().indexOf("!/") + 2); /** 将jar复制一份到当前文件夹 **/ String oldJar = jarPath.substring(jarPath.indexOf("/"),jarPath.lastIndexOf("!/")); String jarFolder = oldJar.substring(0,oldJar.indexOf("blockchaincode")); String copiedJar = jarFolder + "copied.jar"; JarUtil.copyJarByJarFile(new File(oldJar), new File(copiedJar)); /** 解压复制的jar文件 **/ JarUtil.unJarByJarFile(new File(copiedJar), new File(jarFolder)); /** 删除--除了证书文件夹和jar文件的所有文件夹 **/ deleteDir(jarFolder); } //清空文件夹下除了证书文件夹和jar文件的所有文件 private static boolean deleteDir(String path){ File file = new File(path); if(!file.exists()){//判断是否待删除目录是否存在 // System.err.println("The dir are not exists!"); log.error("The dir are not exists!"+file.getAbsolutePath()); return false; } String[] content = file.list();//取得当前目录下所有文件和文件夹 for(String name : content){ File temp = new File(path, name); if(temp.isDirectory()){ //如果是证书文件,不删除 if(name.contains("crypto-config")) continue; deleteDir(temp.getAbsolutePath());//递归调用,删除目录里的内容 temp.delete();//删除空目录 }else{ //如果是jar包,不删除 if(name.contains(".jar")) continue; if(!temp.delete()){//直接删除文件 // System.err.println("Failed to delete " + name); log.error("Failed to delete " + name); } } } return true; } }
package blockchaincode.utils; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Enumeration; import java.util.jar.JarEntry; import java.util.jar.JarFile; import java.util.jar.JarOutputStream; import java.util.zip.ZipEntry; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * jar包压缩跟解压工具类 * @author WangSong * */ public class JarUtil { private static Logger log = LoggerFactory.getLogger(JarUtil.class); private JarUtil(){} /** * 复制jar by JarFile * @param src * @param des * @throws IOException */ public static void copyJarByJarFile(File src , File des) throws IOException{ //重点 JarFile jarFile = new JarFile(src); Enumeration<JarEntry> jarEntrys = jarFile.entries(); JarOutputStream jarOut = new JarOutputStream(new BufferedOutputStream(new FileOutputStream(des))); byte[] bytes = new byte[1024]; while(jarEntrys.hasMoreElements()){ JarEntry entryTemp = jarEntrys.nextElement(); jarOut.putNextEntry(entryTemp); BufferedInputStream in = new BufferedInputStream(jarFile.getInputStream(entryTemp)); int len = in.read(bytes, 0, bytes.length); while(len != -1){ jarOut.write(bytes, 0, len); len = in.read(bytes, 0, bytes.length); } in.close(); jarOut.closeEntry(); log.error("复制完成: " + entryTemp.getName()); } jarOut.finish(); jarOut.close(); jarFile.close(); } /** * 解压jar文件by JarFile * @param src * @param desDir * @throws FileNotFoundException * @throws IOException */ public static void unJarByJarFile(File src , File desDir) throws FileNotFoundException, IOException{ JarFile jarFile = new JarFile(src); Enumeration<JarEntry> jarEntrys = jarFile.entries(); if(!desDir.exists()) desDir.mkdirs(); //建立用户指定存放的目录 byte[] bytes = new byte[1024]; while(jarEntrys.hasMoreElements()){ ZipEntry entryTemp = jarEntrys.nextElement(); File desTemp = new File(desDir.getAbsoluteFile() + File.separator + entryTemp.getName()); if(entryTemp.isDirectory()){ //jar条目是空目录 if(!desTemp.exists()) desTemp.mkdirs(); log.error("makeDir" + entryTemp.getName()); }else{ //jar条目是文件 //因为manifest的Entry是"META-INF/MANIFEST.MF",写出会报"FileNotFoundException" File desTempParent = desTemp.getParentFile(); if(!desTempParent.exists())desTempParent.mkdirs(); BufferedInputStream in = new BufferedInputStream(jarFile.getInputStream(entryTemp)); BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(desTemp)); int len = in.read(bytes, 0, bytes.length); while(len != -1){ out.write(bytes, 0, len); len = in.read(bytes, 0, bytes.length); } in.close(); out.flush(); out.close(); log.error("解压完成: " + entryTemp.getName()); } } jarFile.close(); } }