在 java 中访问网络共享文件夹(位于 Windows 或 Linux)的推荐方法是什么
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30496635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the recommended way of accessing a network share folder (located in Windows or Linux) in java
提问by Joe.wang
All, Forgive me I am not familiar with the Linux. I am trying to read all the files of a network share folder which is located in either Windows or Linux system.
所有,请原谅我对Linux不熟悉。我正在尝试读取位于 Windows 或 Linux 系统中的网络共享文件夹的所有文件。
Currently I just made it work for the case of Windows by below code.
目前我只是通过下面的代码使它适用于 Windows 的情况。
networkShareFolder="\\10.50.90.18\ITS Tool\xml\";//It is a windows Network share path.
File[] files = new File(networkShareFolder).listFiles();
But When I deploy my application to the Linux system and run it. It just told me can not get any files from the specified networkShareFolder
;
但是当我将我的应用程序部署到 Linux 系统并运行它时。它只是告诉我无法从指定的文件中获取任何文件networkShareFolder
;
So I tried to type the path \\10.50.90.18
in the File explorer of Linux like what I did in the windows. To see if the path can be reached from the Linux system. But it just told me Can't locate the \\10.50.90.18
. But I am sure the IP can be ping from the Linux.
所以我尝试\\10.50.90.18
在 Linux 的文件资源管理器中输入路径,就像我在 Windows 中所做的那样。查看从Linux系统是否可以到达该路径。但它只是告诉我Can't locate the \\10.50.90.18
。但我确定 IP 可以从 Linux ping 通。
So my questions are
所以我的问题是
- Why
\\10.50.90.18
can't be accessed in Linux .But can be accessed in Windows. (I am sure their IP are all 10.50.90.*) - What is the best way to access the network share folder from windows or linux ?
- 为什么
\\10.50.90.18
在Linux 中不能访问。但在Windows 中可以访问。(我确定他们的 IP 都是 10.50.90.*) - 从 windows 或 linux 访问网络共享文件夹的最佳方式是什么?
Thanks.
谢谢。
回答by Elliott Frisch
Remote Mount with FUSE
带保险丝的远程安装
It's possible to mount a remote filesystem (generally including SMB/CIFS) with FUSEand samba. That might look something like (assuming you have a mountpoint /windows
)
可以使用FUSE和samba挂载远程文件系统(通常包括 SMB/CIFS)。这可能看起来像(假设您有一个 mountpoint /windows
)
# export USER=efrisch
# export WORKGRP=mygrp
# smbmount //10.50.90.18/ /windows –o username=$USER,workgroup=$WORKGRP
Then you could access your directory (transparently) with
然后你可以访问你的目录(透明地)
new File("/windows/ITS Tool/xml")
Pure Java Solution (with JCIFS)
纯 Java 解决方案(使用 JCIFS)
JCIFSprovides SmbFile
and that provides listFiles()
allowing something like
JCIFS提供SmbFile
并且提供listFiles()
允许类似的东西
SmbFile[] files = new SmbFile("smb://10.50.90.18/ITS Tool/xml/").listFiles();
The linked documentation for SmbFile
does give the full format as
链接的文档SmbFile
确实提供了完整的格式
smb://[[[domain;]username[:password]@]server[:port]/[[share/[dir/]file]]][?param=value[param2=value2[...]]]
smb://[[[domain;]username[:password]@]server[:port]/[[share/[dir/]file]]][?param=value[param2=value2[...]] ]
and it also notes that all SMB URLs that represent workgroups, servers, shares, or directories require a trailing slash '/'.
它还指出,所有代表工作组、服务器、共享或目录的 SMB URL 都需要尾部斜杠“/”。