public static void runExe(String exe,String[] params,StringBuffer sbout) throws IOException{
String cmd = exe;
for (String param : params) {
cmd+=" "+param;
}
//System.out.println(cmd);
Process proc = Runtime.getRuntime().exec(cmd);
BufferedReader stdout = new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = null;
while((line = stdout.readLine())!=null){
sbout.append(line+"\n");
}
}
/**
*
* @param host 需要访问的机器 192.168.152.169
* @param user 用户名
* @param pw 机器密码
* @param testDir 测试目录名
* @return
* @throws IOException
* @throws URISyntaxException
*/
public static boolean netConn(String host,String user,String pw,String testDir) throws IOException, URISyntaxException{
StringBuffer sb = new StringBuffer();
runExe(RunExe.class.getResource("").toURI().getPath()+"res\\initConn.bat", new String[]{"\\\\"+host,user,pw}, sb);
//System.out.println(sb.toString());
if(sb.toString().contains("success")){
Path p = Paths.get("\\\\"+host+"\\"+testDir);
return p.toFile().exists();
}
return false;
}
public static void main(String[] args) {
try {
System.out.println(netConn("10.156.11.86","Administrator","123456", "Users"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
System.out.println("--------");
}
}
注释:利用系统会记录登录状态的特性,使用bat连接到共享的机器,之后的访问就可以使用访问本地文件的方式进行操作