JAVA - 无需挂载即可从共享存储访问文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23055158/
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
JAVA - Access file from shared storage without mount
提问by Ryo
I have done set up a NAS server using NAS4Freeand share a folder at:
我已经使用NAS4Free设置了 NAS 服务器并在以下位置共享了一个文件夹:
\NAS_SERVER_IP/SHARE_FOLDER_NAME
In SHARE_FOLDER_NAME directory contains resource files need to share to multiple clients
在 SHARE_FOLDER_NAME 目录中包含需要共享给多个客户端的资源文件
Now ,from clients , can I using Java to access (read/write) directly file from NAS server without mount shared folder to local clients
现在,从客户端,我可以使用 Java 直接从 NAS 服务器访问(读/写)文件,而无需将共享文件夹挂载到本地客户端
回答by RRM
Copied from here, but changed the api call argument.
从这里复制,但更改了 api 调用参数。
connecting to shared folder in windows with java
String url = "smb://[NAS server-IP or hostname]/file-or-directory-path";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("[company network domain]", "user", "password");
SmbFile dir = new SmbFile(url, auth);
for (SmbFile f : dir.listFiles())
{
System.out.println(f.getName());
}
回答by RRM
For observing file/dir changes using JDK 6, you could use:
要使用 JDK 6 观察文件/目录更改,您可以使用:
For JDK 7, WatchService is part of NIO package:
对于 JDK 7,WatchService 是 NIO 包的一部分:
回答by RRM
Finally, this one works with JDK6as well. This way, we could observe file/dir changes in windows shared drivers without mounting/mapping them as a drive.
最后,这个也适用于JDK6。这样,我们可以观察 Windows 共享驱动程序中的文件/目录更改,而无需将它们安装/映射为驱动器。
I've used following jars in classpath: commons-collections-4.4.0, commons-logging-1.1.2, commons-logging-api-1.1.2, commons-net-3.3, commons-vfs2-2.0, httpclient-4.3.1, Hymanrabbit-standalone-2.6.5, jcifs-1.3.17, jsch-0.1.51
我在类路径中使用了以下 jar:commons-collections-4.4.0、commons-logging-1.1.2、commons-logging-api-1.1.2、commons-net-3.3、commons-vfs2-2.0、httpclient-4.3 .1, Hymanrabbit-standalone-2.6.5, jcifs-1.3.17, jsch-0.1.51
import org.apache.commons.vfs2.FileChangeEvent;
import org.apache.commons.vfs2.FileListener;
import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.FileSystemManager;
import org.apache.commons.vfs2.VFS;
import org.apache.commons.vfs2.impl.DefaultFileMonitor;
public class NFSChangeObserver
{
public static void main(String[] args) throws FileSystemException
{
/** need a non-daemon thread, because <code>DefaultFileMonitor</code> is internally marked as a daemon thread.
*/
Thread t = new Thread(new Runnable() {
@Override
public synchronized void run()
{
try
{
while(1!=2)
wait();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}});
t.start();
FileSystemManager manager = VFS.getManager();
FileObject file = manager.resolveFile("\\[server-hostname]\[directory-path]");
DefaultFileMonitor fm = new DefaultFileMonitor(new FileListener()
{
@Override
public void fileChanged(final FileChangeEvent fileChangeEvt) throws Exception
{
System.out.println("@" + System.currentTimeMillis() + ": " + fileChangeEvt.getFile().getName() + " changed .." );
}
@Override
public void fileCreated(FileChangeEvent fileChangeEvt) throws Exception
{
System.out.println("@" + System.currentTimeMillis() + ": " + fileChangeEvt.getFile().getName() + " created .." );
}
@Override
public void fileDeleted(FileChangeEvent fileChangeEvt) throws Exception
{
System.out.println("@" + System.currentTimeMillis() + ": " + fileChangeEvt.getFile().getName() + " deleted .." );
}
});
fm.setDelay(5000);
fm.addFile(file);
FileObject[] children = file.getChildren();
for(FileObject child : children)
{
System.out.println(child.getURL());
}
fm.start();
}
}