使用Java的映射网络驱动器未连接

使用Java的映射网络驱动器未连接

问题描述:

我试图通过Java使用命令'net use'映射网络驱动器。使用Java的映射网络驱动器未连接

我已经尝试了几种方法,我在网上看到过,但似乎没有在我们的UAT PC上工作,但总是在我的电脑上工作。

UAT PC - 在Windows Server 2008 R2标准版Service Pack 1的 我的电脑 - Windows 7的专业版Service Pack 1的

会发生什么是方法创建一个新的驱动器,但驱动器图标的X标志指示的东西是错误的,当我点击它时,它说: “位置不可用”[信息标题] 无法访问L:。 登录失败:未知的用户名或密码不正确。

以下是我迄今试过的代码,但产生相同的结果。

public void connectToRemoteServer() 
{ 
    try 
    { 
    String USER_NAME = "domain\\username"; 
    String PASSWORD = "password"; 
    /** ATTEMPT 1 - Call command from JAVA using string - OK in LOCAL, FAILED in UAT */ 
    // String commandInside = "net use " + REMOTE_FILE_SERVER_DRIVE + ": \\\\ipaddress\\folder /user:" + USER_NAME + " " + PASSWORD; 
    // Process p = Runtime.getRuntime().exec(commandInside); 
    // Log.info("Executing command [" + commandInside + "]"); 
    // p.waitFor(); 

    /** ATTEMPT 2 - Call command from JAVA using an array of string - OK in LOCAL, FAILED in UAT */ 
    // String[] commandInside = {"c:\\windows\\system32\\net.exe", "use", REMOTE_FILE_SERVER_DRIVE + ":", "\\\\ipaddress\\folder", "/user:" + USER_NAME, PASSWORD }; 
    // Process p = Runtime.getRuntime().exec(commandInside); 
    // Log.info("Executing command [" + commandInside + "]"); 
    // p.waitFor(); 

    /** ATTEMPT 3 - Call command from JAVA using a process builder - OK in LOCAL, FAILED in UAT */ 
    // String[] commandInside = { "cmd.exe", "/C", "net", "use", REMOTE_FILE_SERVER_DRIVE + ":", "\\\\ipaddress\\folder", "/user:" + USER_NAME, PASSWORD }; 
    // Log.info("Constructed command [" + Arrays.toString(commandInside) + "]"); 
    // ProcessBuilder pb = new ProcessBuilder(commandInside); 
    // Log.info("Starting command"); 
    // Process process = pb.start(); 
    // Log.info("Waiting for command to complete"); 
    // process.waitFor(); 

    /** ATTEMPT 4 - Write the command in a cmd/bat file then execute that cmd/bat file from JAVA using an array of string - OK in LOCAL, FAILED in UAT*/ 
    File batchFile = new File(ROOT_PATH + "MapRemoteServer.cmd"); 

    String commandInside = "net use " + REMOTE_FILE_SERVER_DRIVE + ": \\\\ipaddress\\folder/user:" + USER_NAME + " " + PASSWORD; 
    batchFile.createNewFile(); 
    FileWriter fw = new FileWriter(batchFile); 
    fw.write(commandInside + "\r\n\r\nexit"); 
    fw.close(); 

    Thread.sleep(500); // just to ensure that the file is properly closed before running it 
    String[] command = { "cmd.exe", "/c", "Start", ROOT_PATH + "MapRemoteServer.cmd" }; 

    Process process = Runtime.getRuntime().exec(command); 
    process.waitFor(); 


    int exitStatus = process.exitValue(); 
    Log.info("Exit status: [" + exitStatus + "]"); 

    if (exitStatus != 0 && exitStatus != 2) 
    { 
     BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream())); 
     String messageLine = null; 
     Log.info("Error/Warning encountered during execution of net use!"); 
     while ((messageLine = stdError.readLine()) != null) { 
     if (messageLine.length() > 0) 
     { 
      Log.info(messageLine); 
     } 
     } 
    } 
    else if (exitStatus == 2) 
    { 
     Log.info("Connection already established!"); 
    } 
    else 
    { 
     Log.info("Connection successful!"); 
    } 
    } 
    catch (Exception e) 
    { 
    e.printStackTrace(); 
    } 
} 

如果我运行在命令行(从尝试1)的实际命令或我执行批处理文件(来自尝试4)本身,它的工作原理。只有当它由我的Java代码执行时,它不会。

除了网络使用,还有另一种方式来映射通过Java在Windows驱动器? 是否有某种设置可以在Windows PC中完成,可能会阻止从程序映射?

+1

映射驱动器需要管理员访问权限。您需要以较高的安全性运行该命令。 Google ['java以管理员身份运行]](https://www.google.com/search?q=java+run+as+administrator)。 – Andreas

+0

我用来映射驱动器的ID已经具有访问'(/ user:“+ USER_NAME +”“+ PASSWORD)'。你的意思是我还需要使用管理员ID来运行我的批处理脚本或命令? – leahsaif

+0

这是用于访问网络共享的ID,但您需要本地管理员权限来映射驱动器号,因为这被认为是更改了操作系统设置,因此在Windows UAC处于活动状态时运行命令需要提升。 – Andreas

发布答案以供将来参考。 当我执行没有字母的net use命令时,它可以工作。 它不会创建驱动器盘符,但如果我键入完整路径(ipaddress \ folder),则可以访问它。 我使用的命令字符串是

net use " + "\\\\ipaddress\\folder /user:" + USER_NAME + " " + PASSWORD 

......做到了!顺便说一下,我的ID有完全访问权限。

你只需要检查,如果该文件夹存在if (!myDirectory.exists())知道你是否连接。