| 35 | } |
| 36 | |
| 37 | public String[] RunCmd(String[] dotPath){ |
| 38 | boolean isWindowsOS = isWindowsOS(); |
| 39 | String[] cmd; |
| 40 | mkdir(ConstatField.ResultTemp); |
| 41 | ArrayList<String> imgPaths = new ArrayList<>(); |
| 42 | |
| 43 | logger.info(dotPath.length); |
| 44 | for (int i = 0; i < dotPath.length; i++) { |
| 45 | String imgPath = ConstatField.ResultTemp + ConstatField.separator + dotPath[i].substring(dotPath[i].lastIndexOf("\\") + 1, dotPath[i].lastIndexOf(".")) + ".png"; |
| 46 | if (isWindowsOS) { |
| 47 | //windows下的命令 |
| 48 | cmd = new String[]{"cmd.exe", "/c", "dot", "-Tpng", dotPath[i], "-o", imgPath}; |
| 49 | } else { |
| 50 | //linux下的命令 |
| 51 | // fix #issue2 |
| 52 | imgPath = ConstatField.ResultTemp + ConstatField.separator + dotPath[i].substring(dotPath[i].lastIndexOf("/") + 1, dotPath[i].lastIndexOf(".")) + ".png"; |
| 53 | cmd = new String[]{"dot", "-Tpng" , dotPath[i] ,"-o", imgPath}; |
| 54 | } |
| 55 | try { |
| 56 | |
| 57 | logger.info("开始生成图片 " + dotPath[i]); |
| 58 | logger.info("正在执行命令 " + Arrays.toString(cmd)); |
| 59 | sleep(1000); |
| 60 | String charsetName = isWindowsOS ? "GBK" : "UTF-8"; |
| 61 | |
| 62 | byte[] bytes = null; |
| 63 | Scanner runCmd = new Scanner(Runtime.getRuntime().exec(cmd).getInputStream(), charsetName).useDelimiter("\\A"); |
| 64 | // fix #issue2: on linux : detect hasNext otherwise will throw an NoSuchElementException |
| 65 | if (runCmd.hasNext()) { |
| 66 | bytes = runCmd.next().getBytes(charsetName); |
| 67 | } |
| 68 | |
| 69 | File file = new File(imgPath); |
| 70 | if (!file.exists()) { |
| 71 | logger.info("bytes.length = " + bytes.length); |
| 72 | logger.info("something wrong"); |
| 73 | Alert alert = new Alert(Alert.AlertType.WARNING); |
| 74 | alert.setTitle("警告"); |
| 75 | alert.setContentText(new String(bytes, charsetName)); |
| 76 | alert.show(); |
| 77 | } else{ |
| 78 | logger.info("生成图片成功"); |
| 79 | logger.info("图片路径为:" + imgPath); |
| 80 | imgPaths.add(imgPath); |
| 81 | } |
| 82 | } catch (Exception e) { |
| 83 | logger.error("生成图片失败 " + dotPath[i]); |
| 84 | Alert alert = new Alert(Alert.AlertType.ERROR); |
| 85 | alert.setTitle("Error"); |
| 86 | alert.setContentText(e.getMessage()); |
| 87 | alert.show(); |
| 88 | logger.error(e.getMessage()); |
| 89 | } |
| 90 | } |
| 91 | return imgPaths.toArray(new String[0]); |
| 92 | |
| 93 | } |
| 94 | |