2017-09-13 131 views
0

我試圖通過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

回答

0

發佈答案以供將來參考。 當我執行沒有字母的net use命令時,它可以工作。 它不會創建驅動器盤符,但如果我鍵入完整路徑(ipaddress \ folder),則可以訪問它。 我使用的命令字符串是

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

......做到了!順便說一下,我的ID有完全訪問權限。

你只需要檢查,如果該文件夾存在if (!myDirectory.exists())知道你是否連接。

相關問題