如何使用刚刚映射到本地驱动器的网络文件夹?

时间:2012-06-12 23:22:14

标签: java smb shared-directory

this thread和其他人的跟踪中,我已经设置了一个代码块,我将网络驱动器映射到本地驱动器,然后在其中列出一些文件。

问题是:看起来第一次调用中的映射还没有到FileUtils方法,只有当我等待并再次调用例程时,它才会找到准备好使用的映射驱动器。

String path = "B:/Files/Somwething/SomethingElse/FinalDepth";
File newFile = new File("B:/Files/Something/SomethingElse");
if (!newFile.isDirectory()) {
    flagNetwork = true;
    p = Runtime.getRuntime().exec("net use B: " + networkSharedFolder);

    if(p != null) {
        Collection<File> files = FileUtils.listFiles(new File(path), arrExt, false);
        Iterator<File> iterFiles = files.iterator();
        while(iterFiles.hasNext()) {
            File tmpFile = (File) iterFiles.next();
            listResult.add(tmpFile);
        }
        LogServicio.doLog("[MyApplication, ContextListener] Drive B mapped to " + networkSharedFolder + ".", LogServicio.CErrorLvl);
    } else {
        throw new Exception("Error mapping network drive.");
    }
}

¿我应该怎样做才能一次使用映射的驱动器(或者保证我等到驱动器可用)?

1 个答案:

答案 0 :(得分:1)

Runtime.exec启动进程运行,但并不真正等待它。在知道驱动器的映射之前,您必须等待该过程完成(通过类似p.waitFor()之类的过程)。

...
if (p != null) {
    while (true) {
        try { p.waitFor(); break; }
        catch (InterruptedException ex) { /* don't care */ }
    }

    Collection<File> files ...