【问题标题】:How to use a network folder that has just been mapped to a local drive?如何使用刚刚映射到本地驱动器的网络文件夹?
【发布时间】:2012-06-12 23:22:13
【问题描述】:

按照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.");
    }
}

¿我应该怎么做才能立即使用映射的驱动器(或保证我等到驱动器可用)?

【问题讨论】:

  • 没有将驱动器映射到B: 会导致您的第二个软盘驱动器出现问题吗? :-)

标签: java smb shared-directory


【解决方案1】:

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

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

    Collection<File> files ...

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-04
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 1970-01-01
    • 2020-09-24
    • 1970-01-01
    相关资源
    最近更新 更多