当前位置: 首页 > wzjs >正文

企业形象成品网站最好看免费观看高清视频了

企业形象成品网站,最好看免费观看高清视频了,优酷土豆网站建设,重庆网站建设的目的1.问题发现 项目中有个common包资源文件,然后springboot项目引用了common,那么我们要怎么读取这个资源了。这里需要考虑三个场景,idea运行时、common jar独立运行时、springboot引用common后运行时。 2.问题解决 2.1.idea运行时 Protection…

1.问题发现

       项目中有个common包资源文件,然后springboot项目引用了common,那么我们要怎么读取这个资源了。这里需要考虑三个场景,idea运行时、common jar独立运行时、springboot引用common后运行时。

2.问题解决

2.1.idea运行时

 ProtectionDomain protectionDomain = LibPaths.class.getProtectionDomain();CodeSource codeSource = protectionDomain.getCodeSource();if (codeSource != null) {String jarPath = codeSource.getLocation().getPath();jarPath = jarPath.replaceFirst("^nested:", "");if(!jarPath.endsWith(".jar")){System.out.println("idea运行时classes路径");}

2.2.common jar运行时 

public static String getUrl(){ProtectionDomain protectionDomain = LibPaths.class.getProtectionDomain();CodeSource codeSource = protectionDomain.getCodeSource();if (codeSource != null) {String jarPath = codeSource.getLocation().getPath();jarPath = jarPath.replaceFirst("^nested:", "");System.out.println("JAR 文件路径: " + jarPath);if(jarPath != null){int index = jarPath.indexOf(".jar");String destDir = jarPath;if(index != -1){destDir = new File(jarPath.substring(0, index+4)).getParent();}if(jarPath.endsWith(".jar")){copyJarCuToOut(jarPath,destDir)}else{System.out.println("idea运行时classes路径");}}
}public static void copyJarCuToOut(String jarPath,String destDir) throws IOException {File file = new File(destDir + File.separator + "test");if(!file.exists()){file.mkdirs();copyResourcesFromJar(jarPath,destDir, true);}else{System.out.println(file.getPath() + "已存在");}
}/*** 将 JAR 中指定路径下的所有文件复制到目标目录** @param jarFilePath   JAR 文件的路径* @param resourcePath  JAR 中的资源路径(例如 "resources/")* @param destDir       目标输出目录* @throws IOException  如果复制过程中出现错误*/
public static void copyResourcesFromJar(String jarFilePath, String resourcePath, String destDir) throws IOException {try (JarFile jarFile = new JarFile(jarFilePath)) {Enumeration<JarEntry> entries = jarFile.entries();while (entries.hasMoreElements()) {JarEntry entry = entries.nextElement();if (entry.getName().startsWith(resourcePath) && !entry.isDirectory() && ( entry.getName().endsWith(".cu") || entry.getName().endsWith(".cu.h"))) {// 构建目标路径Path destPath = Paths.get(destDir + File.separator + entry.getName().substring(resourcePath.length()));// 复制文件try (InputStream is = jarFile.getInputStream(entry);OutputStream os = Files.newOutputStream(destPath)) {byte[] buffer = new byte[1024];int len;while ((len = is.read(buffer)) > 0) {os.write(buffer, 0, len);}}System.out.println("已复制: " + entry.getName() + " 到 " + destPath);}}}
}

2.3.springboot jar运行时  

public static String getUrl(){ProtectionDomain protectionDomain = LibPaths.class.getProtectionDomain();CodeSource codeSource = protectionDomain.getCodeSource();if (codeSource != null) {String jarPath = codeSource.getLocation().getPath();jarPath = jarPath.replaceFirst("^nested:", "");System.out.println("JAR 文件路径: " + jarPath);if(jarPath != null){int index = jarPath.indexOf(".jar");String destDir = jarPath;if(index != -1){destDir = new File(jarPath.substring(0, index+4)).getParent();}if(jarPath.endsWith(".jar")){copyJarCuToOut(jarPath,destDir)}else{System.out.println("idea运行时classes路径");}}
}public static void copyJarCuToOut(String jarPath,String destDir) throws IOException {File file = new File(destDir + File.separator + "test");if(!file.exists()){file.mkdirs();copyResourcesFromMultipleJar(jarPath,destDir, true);}else{System.out.println(file.getPath() + "已存在");}
}/*** 将 JAR 中指定路径下的所有文件复制到目标目录** @param jarFilePath   JAR 文件的路径* @param resourcePath  JAR 中的资源路径(例如 "resources/")* @param destDir       目标输出目录* @throws IOException  如果复制过程中出现错误*/
public static void copyResourcesFromMultipleJar(String jarFilePath, String resourcePath, String destDir) throws IOException {// 1. 分离路径部分String[] parts = jarFilePath.split("/!");if (parts.length < 2) {throw new IllegalArgumentException("无效的嵌套 JAR 路径格式");}// 2. 解析外层 JAR 路径(去掉开头的 / 和 !/)String outerJarPath = parts[0];String innerJarEntry = parts[1].replace("!/", "");System.out.println("###################" + outerJarPath);// 3. 打开外层 JARtry (JarFile outerJar = new JarFile(outerJarPath)) {// 4. 获取内层 JAR 的 EntryJarEntry innerEntry = outerJar.getJarEntry(innerJarEntry);if (innerEntry == null) {throw new FileNotFoundException("内层 JAR 不存在: " + innerJarEntry);}System.out.println("innerJarEntry###################" + innerJarEntry);try (InputStream innerIs = outerJar.getInputStream(innerEntry); JarInputStream innerJar = new JarInputStream(innerIs)) {JarEntry entry;while ((entry = innerJar.getNextJarEntry()) != null) {if (entry.getName().startsWith(resourcePath) && !entry.isDirectory()) {// 构建目标路径Path destPath = Paths.get(destDir + File.separator + entry.getName().substring(resourcePath.length()));// 复制文件try (OutputStream os = Files.newOutputStream(destPath)) {byte[] buffer = new byte[1024];int len;while ((len = innerJar.read(buffer)) > 0) {os.write(buffer, 0, len);}}System.out.println("已复制: " + entry.getName() + " 到 " + destPath);}}}}}

2.4.完整代码

package com.omega.common.lib;import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.CodeSource;
import java.security.ProtectionDomain;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.jar.JarInputStream;public class LibPaths {static {try {// 获取 ProtectionDomain 和 CodeSourceProtectionDomain protectionDomain = LibPaths.class.getProtectionDomain();CodeSource codeSource = protectionDomain.getCodeSource();if (codeSource != null) {String jarPath = codeSource.getLocation().getPath();jarPath = jarPath.replaceFirst("^nested:", "");System.out.println("JAR 文件路径: " + jarPath);if(jarPath != null){int index = jarPath.indexOf(".jar");String destDir = jarPath;if(index != -1){destDir = new File(jarPath.substring(0, index+4)).getParent();}if(jarPath.endsWith(".jar")){copyJarCuToOut(jarPath,destDir, false);}else if(jarPath.endsWith(".jar!/")){copyJarCuToOut(jarPath,destDir, true);}LIB_PATH = destDir + File.separator + "test";}else{System.out.println("JAR 文件路径为空");}} else {System.out.println("无法获取 JAR 路径(可能来自系统类加载器)");}} catch (IOException e) {e.printStackTrace();}}public static void copyJarCuToOut(String jarPath,String destDir, boolean multiple) throws IOException {File file = new File(destDir + File.separator + "test");if(!file.exists()){file.mkdirs();if(multiple){copyResourcesFromMultipleJar(jarPath, "test", file.getPath());}else{copyResourcesFromJar(jarPath, "test", file.getPath());}}else{System.out.println(file.getPath() + "已存在");}}/*** 将 JAR 中指定路径下的所有文件复制到目标目录** @param jarFilePath   JAR 文件的路径* @param resourcePath  JAR 中的资源路径(例如 "resources/")* @param destDir       目标输出目录* @throws IOException  如果复制过程中出现错误*/public static void copyResourcesFromJar(String jarFilePath, String resourcePath, String destDir) throws IOException {try (JarFile jarFile = new JarFile(jarFilePath)) {Enumeration<JarEntry> entries = jarFile.entries();while (entries.hasMoreElements()) {JarEntry entry = entries.nextElement();if (entry.getName().startsWith(resourcePath) && !entry.isDirectory() && ( entry.getName().endsWith(".cu") || entry.getName().endsWith(".cu.h"))) {// 构建目标路径Path destPath = Paths.get(destDir + File.separator + entry.getName().substring(resourcePath.length()));// 复制文件try (InputStream is = jarFile.getInputStream(entry);OutputStream os = Files.newOutputStream(destPath)) {byte[] buffer = new byte[1024];int len;while ((len = is.read(buffer)) > 0) {os.write(buffer, 0, len);}}System.out.println("已复制: " + entry.getName() + " 到 " + destPath);}}}}/*** 将 JAR 中指定路径下的所有文件复制到目标目录** @param jarFilePath   JAR 文件的路径* @param resourcePath  JAR 中的资源路径(例如 "resources/")* @param destDir       目标输出目录* @throws IOException  如果复制过程中出现错误*/public static void copyResourcesFromMultipleJar(String jarFilePath, String resourcePath, String destDir) throws IOException {// 1. 分离路径部分String[] parts = jarFilePath.split("/!");if (parts.length < 2) {throw new IllegalArgumentException("无效的嵌套 JAR 路径格式");}// 2. 解析外层 JAR 路径(去掉开头的 / 和 !/)String outerJarPath = parts[0];String innerJarEntry = parts[1].replace("!/", "");System.out.println("###################" + outerJarPath);// 3. 打开外层 JARtry (JarFile outerJar = new JarFile(outerJarPath)) {// 4. 获取内层 JAR 的 EntryJarEntry innerEntry = outerJar.getJarEntry(innerJarEntry);if (innerEntry == null) {throw new FileNotFoundException("内层 JAR 不存在: " + innerJarEntry);}System.out.println("innerJarEntry###################" + innerJarEntry);try (InputStream innerIs = outerJar.getInputStream(innerEntry); JarInputStream innerJar = new JarInputStream(innerIs)) {JarEntry entry;while ((entry = innerJar.getNextJarEntry()) != null) {if (entry.getName().startsWith(resourcePath) && !entry.isDirectory()) {// 构建目标路径Path destPath = Paths.get(destDir + File.separator + entry.getName().substring(resourcePath.length()));// 复制文件try (OutputStream os = Files.newOutputStream(destPath)) {byte[] buffer = new byte[1024];int len;while ((len = innerJar.read(buffer)) > 0) {os.write(buffer, 0, len);}}System.out.println("已复制: " + entry.getName() + " 到 " + destPath);}}}}}}

http://www.dtcms.com/wzjs/347545.html

相关文章:

  • 东莞樟木头做网站哪家好windows优化大师官网
  • 动画设计就业前景优化营商环境个人心得
  • 淘宝上做网站权重磁力宝最佳搜索引擎入口
  • 网站搜索引擎优化怎么做建设企业营销型网站
  • 做网站需要服务器么苏州seo按天扣费
  • 企业cms建站站长之家网站排行榜
  • go 语言 做网站谷歌浏览器下载安装2021最新版
  • 网页浏览器是windows系统自带的是资源网站优化排名软件
  • 设计公司logo网站徐州百度seo排名优化
  • 坐什么网站能用到html5百度广告怎么收费标准
  • 做网站用虚拟服务器可以吗云速seo百度点击
  • 广东微信网站制作价格百度seo和谷歌seo有什么区别
  • 久霸高端网页版网站首页关键词如何优化
  • 深圳美食教学网站制作网站推广的方式有哪些
  • 涉县手机网站建设营销型网站建设实训总结
  • 怎么做网站上翻译泰剧企业网络规划与设计
  • 一万元做网站自助建站申请
  • 做设计的网站定制放单平台
  • 邯郸网站制作咨询热线网站关键词排名seo
  • 广州市企业网站建设淘宝引流推广平台
  • 如何做网站的订阅太原seo霸屏
  • 做微网站的第三方平台有哪些网络推广方法怎么做
  • 最好的网站模板网站百度电话销售
  • apmserv搭建网站关键词英文
  • wordpress判断手机版西安seo关键词推广
  • 网站首页栏目怎么做网络营销方案模板
  • 广州网络服装网站建设兰州网站开发公司
  • 中色冶金建设有限公司网站西安百度公司开户
  • 临猗网站制作互联网推广销售是做什么的
  • app软件开发公司如何选择广州seo网站多少钱