* Solution: After the project is packaged into a jar, the certificate file under the resources path cannot be accessed.
* Ideas:
* 1. Copy a jar at runtime
* 2. Unzip the copied jar to the jar file directory
* 3. Delete the copied jar. Uncompressed non-certificate folder
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; /** * * When independently developing HKDAS applications (Java project, Maven project), use this method to start the application. * @author WangSong * */ @DasBootApplication() public class DasApplicationBootstrap { private static Logger log = LoggerFactory.getLogger (DasApplicationBootstrap. class ); public static void main (String [] args) { // Copy the certificate file to the same directory of the project try { CryptoUtil.pass(); } catch (Exception e) { e.printStackTrace (); log.error ( "The copy of the certificate file in the current jar package directory is abnormal!" , e); System.err.println ( "The certificate file copy is abnormal!" ); } // 启动 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; /** * Solution: When the project is packaged as a jar, the certificate file under the resources path cannot be accessed * Ideas: * 1. Copy a jar at runtime * 2. Unzip the copied jar to the jar file directory * 3. Delete the copied jar and uncompressed non-certificate folder * @author WangSong */ public class CryptoUtil { private static Logger log = LoggerFactory.getLogger(CryptoUtil.class); private CryptoUtil(){} public static void pass () throws Exception { / ** Get current jar location * * / URL url = Main. class .getClassLoader (). getResource ("crypto-config /" ); // Currently need to be a jar file String protocol = url.getProtocol (); // Probably jar if (! "jar" .equalsIgnoreCase (protocol)) { log.error ( "Not run as jar, do not generate certificate file repeatedly." ); 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 ); / ** Copy jar to the current folder * * / String oldJar = jarPath.substring (jarPath.indexOf ("/"), jarPath.lastIndexOf (" ! / " )); String jarFolder = oldJar.substring(0,oldJar.indexOf("blockchaincode")); String copiedJar = jarFolder + "copied.jar"; JarUtil.copyJarByJarFile ( new new File (oldJar), new new File (copiedJar)); / ** decompression copy jar file * * / JarUtil.unJarByJarFile ( new new File (copiedJar), new new File (jarFolder)); / ** Delete - -All folders except the certificate folder and jar files * * / deleteDir(jarFolder); } // Empty all files in the folder except the certificate folder and jar file private static boolean deleteDir (String path) { File file = new File (path); if (! File.exists ()) { // Determine whether the directory to be deleted exists // System.err.println ("The dir are not exists!"); Log.error ( "The dir are not exists!" + File.getAbsolutePath ()); return false ; } String [] content = file.list (); // Get all files and folders in the current directory for (String name: content) { File temp = new File (path, name); if (temp.isDirectory ()) { // If it is a certificate file, do not delete if (name.contains ("crypto-config" )) continue ; deleteDir (temp.getAbsolutePath ()); // Recursive call, delete the contents of the directory temp.delete (); // Delete the empty directory } else { // If it is a jar package, do not delete if (name.contains (". jar " )) continue ; if (! temp.delete ()) { // Delete files directly // 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 package compression and decompression tools * @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 ( "Copy completed:" + entryTemp.getName ()); } jarOut.finish(); jarOut.close(); jarFile.close(); } /** * Unzip jar file 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 (); // Create a directory specified by the user 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 ()) { // The jar entry is an empty directory if (! desTemp.exists ()) desTemp.mkdirs (); log.error("makeDir" + entryTemp.getName()); } else { // The jar entry is a file // Because the entry of the manifest is "META-INF / MANIFEST.MF", write the report "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 ( "Decompression completed:" + entryTemp.getName ()); } } jarFile.close(); } }