当前位置: 代码迷 >> 综合 >> SpringBoot读取jar包中的资源文件
  详细解决方案

SpringBoot读取jar包中的资源文件

热度:70   发布时间:2024-02-28 20:34:04.0

 java -jar启动读取jar包内部资源文件

    /*** 读取jar包中classpath下面的文件** @param path 文件名* @return*/public static String readJarFile(String path) {String s = null;try {ClassPathResource cpr = new ClassPathResource(path);System.out.println(cpr.getURL());byte[] bdata = FileCopyUtils.copyToByteArray(cpr.getInputStream());s = new String(bdata, StandardCharsets.UTF_8);} catch (IOException e) {e.printStackTrace();}return s;}

获取jar包的路径,用于配置文件

    public static String getJarPath() {ApplicationHome h = new ApplicationHome(PathUtil.class);File jarF = h.getSource();System.out.println(jarF.getParentFile().toString());return jarF.getParentFile().toString()+"/";}