package utils; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * @author gaofeng */ public class CmdUtils { public static void main(String[] args) throws IOException, InterruptedException { /
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @author gaofeng
*/
public class CmdUtils {
public static void main(String[] args) throws IOException, InterruptedException {
// TODO Auto-generated method stub
CmdUtils.runCMD("start http://img.558idc.com/uploadfile/allimg/java/v2-8a0bd5aa5d246402db25ccaeac2e2a21_hd.jpg");
}
/**
* 启动网页
*
* @param htmlPath 网页地址(url)
*/
public static void startHtml(String htmlPath) {
if ((htmlPath.startsWith("http") || htmlPath.startsWith("wwww.")) &&
(htmlPath.endsWith(".com") || htmlPath.endsWith(".com") || htmlPath.endsWith(".cn")
|| htmlPath.endsWith(".js") || htmlPath.endsWith(".jpg") || htmlPath
.endsWith(".png"))) {
try {
runCMD("start " + htmlPath);
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("htmlPath格式不对");
}
}
/**
* @param cmd 执行命令
*/
public static void runCMD(String cmd) {
final String METHOD_NAME = "runCMD";
BufferedReader br = null;
try {
Process p = Runtime.getRuntime().exec("cmd.exe /C " + cmd);
// Process p = Runtime.getRuntime().exec(cmd);
// br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
br = new BufferedReader(new InputStreamReader(p.getInputStream()));
String readLine = br.readLine();
StringBuilder builder = new StringBuilder();
while (readLine != null) {
readLine = br.readLine();
builder.append(readLine);
}
XLog.showLogInfo(METHOD_NAME + "#readLine: " + builder.toString());
p.waitFor();
int i = p.exitValue();
XLog.showLogInfo(METHOD_NAME + "#exitValue = " + i);
} catch (Exception e) {
XLog.showLogInfo(METHOD_NAME + "#ErrMsg=" + e.getMessage());
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}