gpt4 book ai didi

java - 如何从java应用程序访问映射的网络驱动器?

转载 作者:行者123 更新时间:2023-12-02 10:08:32 24 4
gpt4 key购买 nike

我有一个网络驱动器映射为:

net use h:\ip\服务器名

我有一个普通的 java 应用程序,它将从该驱动器读取 txt 文件。

代码:

File file = new File("H:\\MyFile.txt");

try {
byte[] bytes = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
} catch (IOException ex) {
Logger.getLogger(JavaApplication25.class.getName()).log(Level.SEVERE, null, ex);
}

当我从我的计算机(Windows 7 或另一台装有 Windows Vista 的计算机)运行此程序时,程序运行没有任何问题。

但是,当我从 Windows 10 计算机上运行此程序时,出现以下错误:

java.nio.file.NoSuchFileException: H:\MyFile.txt

但是如果我使用以下代码运行应用程序,它就可以工作:

File file = new File("\\ip\servername\MyFile.txt");

try {
byte[] bytes = Files.readAllBytes(Paths.get(file.getAbsolutePath()));
} catch (IOException ex) {
Logger.getLogger(JavaApplication25.class.getName()).log(Level.SEVERE, null, ex);
}

如何使用映射的驱动器号而不是使用完整地址?

最佳答案

遇到同样的问题。通过编辑Windows注册表解决

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System]
"EnableLinkedConnections"=dword:00000001

如果有效,则表明您的 IT 部门正在这样做。应该管理更好的网络策略。

关于java - 如何从java应用程序访问映射的网络驱动器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55158130/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com