项目场景:

项目中使用了word转pdf等相关的功能,此处遇见的bug,上一篇的文档的解决方案有点low,此文章介绍直接读取jar包中的内容


解决方案:

这里我直接上接口吧,此接口内参杂的些许的业务,大佬们自行抽取核心代码即可

private static void copyFont(String jarPath  ){
    
        File jarfile = new File(jarPath);

        try {
            URL url = getURLForJarPath(jarPath);
            JarURLConnection jarCon = (JarURLConnection) url.openConnection();
            JarFile jarFile  = jarCon.getJarFile();
            Enumeration<JarEntry> jarEntrys = jarFile.entries();
            List<String> list = new ArrayList<String>();
            FileOutputStream fileOut = null;
            String filePath = null;
            while (jarEntrys.hasMoreElements()) {
                JarEntry entry = jarEntrys.nextElement();
                String jarEntryPath = entry.getName();
                String jarEntryName=jarEntryPath.substring(jarEntryPath.lastIndexOf("/"));
                if (jarEntryPath.contains("/biz/wordtopdf/fonts") && !entry.isDirectory()) {
                    list.add(jarEntryName);
                    filePath = getWebRoot() + File.separator + "swordweb" + File.separator + "biz" + File.separator
                            + "wordtopdf" + File.separator + "fonts";
                    if (!new File(filePath).exists()) {
                        logger.debug(filePath,"目录不存在,进行创建!");
                        new File(filePath).mkdirs();
                    }
                    InputStream input = jarFile.getInputStream(entry);
                    filePath += jarEntryName;
                    File baseDir = new File(filePath);
                    //做校验判断该路径下文件是否存在
                    if (!baseDir.exists()) {
                        fileOut = new FileOutputStream(filePath);
                        fileOut.write(convertByte(input));
                        input.close();
                    }
                }
               
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }