【问题标题】:How to access windows mounted drive with Java?如何使用 Java 访问安装在 Windows 上的驱动器?
【发布时间】:2019-09-15 10:10:58
【问题描述】:

我家里有一台 WD My Cloud。我可以在文件资源管理器中使用笔记本电脑访问它,只需输入Z:\path\to\image.jpg

我正在编写一个 Java 程序来读取位于这个小云上的图像内容。

这是我的代码:

Path p =Paths.get("\\\\192.168.1.2\\z$\\path\\to\\image.jpg");

try {
    byte[] data = Files.readAllBytes(p);

    // Process data here...
} catch (IOException e) {
    // ...
}

此程序失败,出现以下异常:

java.nio.file.FileSystemException: \\192.168.1.2\z$\path\to\image.jpg: Nom de réseau introuvable。

“Nom de réseau introuvable”:“未找到网络名称”

我尝试了其他路径但没有成功:

  • \\localhost\z$\path\to\image.jpg
  • \\127.0.0.1\z$\path\to\image.jpg
  • \\MyPcName-PC\z$\path\to\image.jpg
  • Z:/path/to/image.jpg

我错过了什么?

Windows 10
Java 8

【问题讨论】:

  • UNC 还是 SMB?由于安全管理系统的变化,我在 Windows 8+ 上遇到了 SMB 问题
  • @MadProgrammer 我尝试github.com/AgNO3/jcifs-ng 没有成功:类似于SmbFile sf = new SmbFile(pathHere)。此构造函数已弃用,我没有找到带有非弃用构造函数的示例。今天,我读到Path 可以处理这样的路径。到目前为止,它对我不起作用。

标签: java windows path unc


【解决方案1】:

在 Paths.get 参数中格式化字符串,如下所示。

package access.mounted.drive;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.io.IOException;

/**
 * @author Charles
 */
public class AccessMountedDrive {

    public static void main(String[] args) {
        Path p =
        Paths.get("E:\\\\Photos Grandparents\\Scan0166a Rose Pincoffs.jpg");

        try {
            int i=0;
            byte[] data = Files.readAllBytes(p);
            System.out.println("File size in bytes:" + data.length);
            // Process data here...
        } catch (IOException e) {
            System.out.println("IOException: " + e);
        }
    }

}

【讨论】:

  • 代码引发了这个异常:IOException: java.nio.file.NoSuchFileException: Z:\path\to\image.jpg
  • 您找到解决方案了吗?我已经挂载了目录。您是如何执行文件操作的?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-12-23
  • 2015-09-12
  • 1970-01-01
  • 2020-05-06
  • 1970-01-01
  • 1970-01-01
  • 2011-09-11
相关资源
最近更新 更多