Chinaunix首页 | 论坛 | 博客
  • 博客访问: 518040
  • 博文数量: 260
  • 博客积分: 10435
  • 博客等级: 上将
  • 技术积分: 1939
  • 用 户 组: 普通用户
  • 注册时间: 2009-11-24 14:50
文章分类

全部博文(260)

文章存档

2011年(22)

2010年(209)

2009年(29)

我的朋友

分类: Java

2010-09-06 11:24:41

package com.taobao.lottery.biz.core.communicate.bjfc.util;


import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import com.alibaba.common.logging.Logger;
import com.alibaba.common.logging.LoggerFactory;
 

public class ShellFileDown {
    public static Logger logger = LoggerFactory.getLogger(ShellFileDown.class);
   
    private Process pro = null;
    private Runtime runTime = null;

    public ShellFileDown() {
        runTime = Runtime.getRuntime();
        if (runTime == null) {
            logger.error("Create runtime false!");
            runTime.exit(1);
        }
    }

    public int execueteCommand(String command) throws InterruptedException {   
       
        logger.warn("Execute command :" + command);
        int result = -1;
        try {
            pro = runTime.exec(command);
            BufferedReader input = new BufferedReader(new InputStreamReader(pro
                    .getInputStream()));// 这个输入流是获取shell输出的
            PrintWriter output = new PrintWriter(new OutputStreamWriter(pro
                    .getOutputStream()));// 这个输出流主要是对Process进行输入控制用的
            String line;
            while ((line = input.readLine()) != null) {
                logger.warn(line);
                if (-1 != line.indexOf("your name")) {// 当检测到提示输入时,则执行输入操作
                    output.print("liuwei\r\n");// \r\n 不可少,否则相当于没有Enter操作
                    output.flush();// 输入完成之后一定要flush.否则一直处在等待输入的地方
                }

            }
         
            input.close();
            output.close();

            pro.destroy();
            result = 0;
            //result = pro.exitValue();
        } catch (IOException ex) {
           
            logger.error("ShellFileDown.execueteCommand exception" + ex.getMessage());
        }
        return result;
    }
   
   
/*
#!/bin/bash
wget -c ftp://${3}:${4}@${1}:${2}/${5}/${6}    -O ${7}
*/   


在java中调用操作系统的程序时,可以使用java.lang.Runtime.getRuntime().exec() 来实现,但是这个方法在调用命令后就直接返回当前线程了;程序设计时,有时候需要在等待调用的系统程序完成操作后,当前线程才能做下一步操作,此时可以用 类Process的方法waitFor()来实现,它会阻塞当先线程直至调用程序运行结束
Java代码
  1. java.lang.Process process = java.lang.Runtime.getRuntime().exec("");//执行命令生成cube  
  2. try {  
  3.    process.waitFor();  
  4. catch (InterruptedException e) {  
  5. // TODO Auto-generated catch block  
  6.    e.printStackTrace();  
  7. }  
  8. proc_stat = checkFileSize() ? "1" : "3";  


Process的方法waitFor()介绍,取自API文档:
waitFor
public abstract int waitFor()
                     throws InterruptedException导致当前线程等待,如果必要,一直要等到由该 Process 对象表示的进程已经终止。如果已终止该子进程,此方法立即返回。如果没有终止该子进程,调用的线程将被阻塞,直到退出子进程。

返回:
进程的出口值。根据惯例,0 表示正常终止。
抛出:
InterruptedException - 如果当前线程在等待时被另一线程 中断,则停止等待,抛出 InterruptedException。

阅读(660) | 评论(1) | 转发(0) |
0

上一篇:ununtu 中文输入法

下一篇:tcpdump

给主人留下些什么吧!~~

chinaunix网友2010-09-07 09:00:57

Download More than 1000 free IT eBooks: http://free-ebooks.appspot.com