使用 Java 的映射网络驱动器未连接
Mapped Network Drive using Java is Not Connected
我正在尝试通过 Java 使用命令 'net use' 映射网络驱动器。
我尝试了几种我在网上看到的方法,但 none 似乎在我们的 UAT PC 上有效,但在我的 PC 上始终有效。
UAT PC - WindowsServer 2008 R2 Standard Service Pack 1。
我的电脑 - Windows 7 Pro Service Pack 1
发生的情况是该方法创建了一个新驱动器,但驱动器图标有一个 X 标记,表示出现问题,当我单击它时,它显示:
"Location is unavailable" [留言标题]
无法访问 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 中完成的设置可能会阻止程序映射?
发布答案以供将来参考。
当我执行没有字母的 net use 命令时,它起作用了。
它不会创建驱动器号,但如果我键入完整路径 (ipaddress\folder),我就可以访问它。
我使用的命令字符串是
net use " + "\\ipaddress\folder /user:" + USER_NAME + " " + PASSWORD
...成功了!顺便说一句,我的ID有完整的访问权限。
您只需检查该文件夹是否存在 if (!myDirectory.exists())
即可知道您是否已连接。
我正在尝试通过 Java 使用命令 'net use' 映射网络驱动器。
我尝试了几种我在网上看到的方法,但 none 似乎在我们的 UAT PC 上有效,但在我的 PC 上始终有效。
UAT PC - WindowsServer 2008 R2 Standard Service Pack 1。 我的电脑 - Windows 7 Pro Service Pack 1
发生的情况是该方法创建了一个新驱动器,但驱动器图标有一个 X 标记,表示出现问题,当我单击它时,它显示: "Location is unavailable" [留言标题] 无法访问 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 中完成的设置可能会阻止程序映射?
发布答案以供将来参考。 当我执行没有字母的 net use 命令时,它起作用了。 它不会创建驱动器号,但如果我键入完整路径 (ipaddress\folder),我就可以访问它。 我使用的命令字符串是
net use " + "\\ipaddress\folder /user:" + USER_NAME + " " + PASSWORD
...成功了!顺便说一句,我的ID有完整的访问权限。
您只需检查该文件夹是否存在 if (!myDirectory.exists())
即可知道您是否已连接。