3089589

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

碰到一个在上传完图片后需要将其转换为多种格式的问题,当然在Java程序里可以解决,但灵活性不高,所以就想到了用Process来调用外部命令linux的一个工具ImageMagick来解决问题。

waitFor() 导致当前线程等待,如有必要,一直要等到由该 Process 对象表示的进程已经终止。

在调用该方法时,经常会出现线程阻塞,Process需要向主线程汇报运行状态,要注意清空缓存区,即Process的InputStream与ErrorStream

Process p = Runtime.getRuntime().exec("cmd /c dir");
        BufferedReader brinput = new BufferedReader(new InputStreamReader(p.getInputStream()));
        try {
            String lineB = null;
            while ((lineB = brinput.readLine()) != null) {
                System.out.println(new String(lineB));

            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        BufferedReader brerror = new BufferedReader(new InputStreamReader(p.getErrorStream()));
        try {
            String lineB = null;
            while ((lineB = brerror.readLine()) != null) {
                System.out.println(new String(lineB));

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

        p.waitFor();
        System.out.println("123");
posted on 2013-03-26 09:24  liangge0218  阅读(1004)  评论(0编辑  收藏  举报