前言,我对流理解简直是太差劲了,恰好在java吧里发现了一个帖子介绍调用cmd命令,因而,我就跟着敲了,源程序是会调用一个组件来传输命令,我直接写成了控制太输入,也许之后能够改为其余方式,为何一看到这类调用,我就会想到网络web攻击力的大马和小马呢。。。java
源码贴下//web
myTest 类网络
import java.io.IOException; import java.io.PrintWriter; import java.util.Scanner; import javax.swing.JOptionPane; public class myTest { public static void main(String[] args) throws IOException{ Process process=Runtime.getRuntime().exec("cmd"); PrintWriter out =new PrintWriter(process.getOutputStream()); (new myThread(process.getInputStream())).start(); (new myThread(process.getErrorStream())).start(); //String ml = JOptionPane.showInputDialog("请输入CMD命令(q - 退出):"); 调用本地组件 String ml = (new Scanner(System.in)).nextLine(); while(ml!=null&&!ml.equals("q")){ out.println(ml); out.flush(); //ml = JOptionPane.showInputDialog("请输入CMD命令(q - 退出):"); ml = (new Scanner(System.in)).nextLine(); // ml = sc.next(); } System.out.println("退出。。。。。"); System.exit(0); } }
myThread 类ide
import java.io.InputStream; public class myThread extends Thread{ private InputStream is; public myThread(InputStream is){ this.is=is; } @Override public void run() { // TODO Auto-generated method stub super.run(); byte[] buf=new byte[1024]; int size; while(true){ try{ while((size = is.read(buf))!=-1){ System.out.println(new String(buf,0,size,"gbk")); } }catch (Exception e) { e.printStackTrace(); break; } } } }
最后贴下此类最原始的代码地址 http://www.oschina.net/code/snippet_1244036_23600this