* 解決:當項目打包成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(); } }