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

时间:2017-09-13 06:05:36

标签: java windows-server-2008 network-drive

我正在尝试使用命令'net use'通过Java映射网络驱动器。

我尝试了几种我在网上看过的方法,但似乎没有一种方法可以在我们的UAT PC上运行,但总能在我的电脑上运行。

UAT PC - Windows Server 2008 R2标准Service Pack 1。 我的电脑 - Windows 7 Pro 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 个答案:

答案 0 :(得分:0)

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

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

......诀窍!顺便提一下,我的ID具有完全访问权限。

您只需检查该文件夹是否存在if (!myDirectory.exists())即可知道您是否已连接。