拨号
Java代码 :
- 语法: rasdial 连接名称 username password
- 实例: rasdial 我的宽带 hzhz1234567890 dfdfdfdfdf
断网
Java代码
- 语法:rasdial 连接名称 /disconnect
- 实例: rasdial 宽带 /disconnect
java程序调用rasdial命令:(其中读取CMD返回消息时可能会乱码,注意红色字体那一句的编码设置)
Java代码
package
com.sesame.network;
import
java.io.BufferedReader;
import
java.io.InputStreamReader;
public
class
ConnectNetWork {
/**
* 执行CMD命令,并返回String字符串
*/
public
static
String executeCmd(String strCmd)
throws
Exception {
Process p
=
Runtime.getRuntime().exec(
"
cmd /c
"
+
strCmd);
StringBuilder sbCmd
=
new
StringBuilder();
BufferedReader br
=
new
BufferedReader(
new
InputStreamReader(p
.getInputStream(),"GB2312"
)); //这里很重要,设置GB2312解决乱码!!!
//如果程序默认编码就是GB2312,可以不写
//我NetBeans默认用UTF8
String line;
while
((line
=
br.readLine())
!=
null
) {
sbCmd.append(line
+
"
\n
"
);
}
return
sbCmd.toString();
//如果整个过程换成这样,就更清楚了。getInputStream是获取最原始的字节流,
//cmd返回的是以GB2312双字节编码的字节流
InputStream in = p.getInputStream();
byte[] b = new byte[1000];
in.read(b);
String msg = new String(b,"GB2312"); //用GB2312解释这堆字节,就可以组装成一个正常的String了
//如果上边不写GB2312,等于这里用UTF8组装,结果一样
}
/**
* 连接ADSL
*/
public
static
boolean
connAdsl(String adslTitle, String adslName, String adslPass)
throws
Exception {
System.out.println(
"
正在建立连接.
"
);
String adslCmd
=
"
rasdial
"
+
adslTitle
+
"
"
+
adslName
+
"
"
+
adslPass;
String tempCmd
=
executeCmd(adslCmd);
//
判断是否连接成功
if
(tempCmd.indexOf(
"
已连接
"
)
>
0
) {
System.out.println(
"
已成功建立连接.
"
);
return
true
;
}
else
{
System.err.println(tempCmd);
System.err.println(
"
建立连接失败
"
);
return
false
;
}
}
/**
* 断开ADSL
*/
public
static
boolean
cutAdsl(String adslTitle)
throws
Exception {
String cutAdsl
=
"
rasdial
"
+
adslTitle
+
"
/disconnect
"
;
String result
=
executeCmd(cutAdsl);
if
(result.indexOf(
"
没有连接
"
)
!=-
1
){
System.err.println(adslTitle
+
"
连接不存在!
"
);
return
false
;
}
else
{
System.out.println(
"
连接已断开
"
);
return
true
;
}
}
public
static
void
main(String[] args)
throws
Exception {
connAdsl(
"
宽带
"
,
"
hzhz**********
"
,
"
******
"
);
Thread.sleep(
1000
);
cutAdsl(
"
宽带
"
);
Thread.sleep(
1000
);
//
再连,分配一个新的IP
connAdsl(
"
宽带
"
,
"
hzhz**********
"
,
"
******
"
);
}
}
执行结果:
本文介绍了如何使用Java代码调用rasdial命令进行网络拨号与断网操作,包括具体语法示例及Java程序实现,确保网络连接的灵活控制。

1073

被折叠的 条评论
为什么被折叠?



