我尝试了几种我在网上见过的方法,但似乎没有一种在我们的UAT PC上有效,但总是在我的PC上有效。
UAT PC-Windows Server 2008 R2标准Service Pack 1。
我的电脑-Windows 7 Pro Service Pack 1
“位置不可用”[消息标题]
无法访问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();
}
}
除了网络使用之外,还有其他方法可以通过Java在windows中映射驱动器吗?
是否有一些可以在Windows PC中完成的设置可能会阻止程序映射?