java調用CMD命令

前言,我對流理解簡直是太差勁了,恰好在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

相關文章
相關標籤/搜索