package com.davint.util.wifi; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class GetWifi { private static final Log logger=LogFactory.getLog(GetWifi.class.getName()); private static final GetWifi gw= new GetWifi(); public static GetWifi getInstance (){ return gw; } public GetWifi() { // TODO Auto-generated constructor stub } /** * isload * @param cmd */ private void isLoad(String cmd){ Process p; try { p = Runtime.getRuntime().exec(cmd); BufferedReader in = new BufferedReader(new InputStreamReader(p .getInputStream())); String temp = null; StringBuffer strBuffer = new StringBuffer(); while ((temp = (in.readLine())) != null) strBuffer.append(temp); logger.info(strBuffer.toString()); } catch (IOException e) { e.printStackTrace(); } } public void open(){ String cmd="netsh wlan start hostednetwork"; this.isLoad(cmd); } public void close(){ String cmd="netsh wlan stop hostednetwork"; this.isLoad(cmd); } public void help(){ logger.info("输入quit或exit 退出"); logger.info("输入 -h 或help 帮助"); } public void isConnect(){ // 控制台 BufferedReader bin = new BufferedReader(new InputStreamReader(System.in)); String command = ""; String commstr=""; logger.info("please input ssid and key. 输入格式为: ssid,key (hellowifi,123123)"); gw.help(); while (true) { try { command = bin.readLine(); if (command.toLowerCase().equals("quit") || command.toLowerCase().equals("exit")) { logger.info("退出"); System.exit(0); } else if( command.toLowerCase().equals("-h")||command.toLowerCase().equals("help")){ gw.help(); }else { //logger.info(command.toString().toLowerCase()); if(command.length()==0){ gw.help(); logger.info("输入格式为: ssid,key (hellowifi,123123)"); }else{ String[] comm=command.split(","); if(comm.length!=2){ logger.info("输入格式为: ssid,key (hellowifi,123123) 有且仅有一个','"); }else if(comm.length==2){ String ssid=comm[0]; String key=comm[1]; logger.info("ssid :"+ssid+" key:"+key); logger.info("设置承载网络"); StringBuffer sb = new StringBuffer("netsh wlan set hostednetwork mode=allow "); sb.append(" ssid="+ssid); sb.append(" key="+key); commstr=sb.toString(); logger.info(commstr); if(commstr.indexOf("必须使用管理员权限")==-1){ logger.info("必须使用管理员权限!"); break; }else{ //执行 gw.isLoad(commstr); logger.info("请选择打开或关闭WIFI:start打开,close关闭"); if(command.toLowerCase().equals("open")){ //dakai gw.open(); }else if(command.toLowerCase().equals("close")){ //guanbi gw.close(); }else{ logger.info("start打开,close关闭"); } } } } } } catch (IOException e) { logger.info("input error,please try again!"); } } } /** * @param args */ public static void main(String[] args) { gw.isConnect(); } }