1

I have done set up a NAS server using NAS4Free and share a folder at:

\\NAS_SERVER_IP/SHARE_FOLDER_NAME

In SHARE_FOLDER_NAME directory contains resource files need to share to multiple clients

Now ,from clients , can I using Java to access (read/write) directly file from NAS server without mount shared folder to local clients

Ryo
  • 995
  • 2
  • 25
  • 41

3 Answers3

1

Copied from here, but changed the api call argument.

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());
    }
Community
  • 1
  • 1
RRM
  • 2,495
  • 29
  • 46
  • the 1st argument to NtlmPasswordAuthentication could be null if there is no internal network domain for windows logon. Tyically, employees account are actually like \\domain\empLoginId – RRM Apr 14 '14 at 08:50
  • Thanks .It's work for me ,now i finding a solution for dynamic watching remote folder .It mean that ,if any change from file at NAS server , it will be reflect to client ,can jcifs do my work ? – Ryo Apr 14 '14 at 09:11
  • yes, the minimum required jars are: commons-vfs2-2.0, jcifs-1.3.17, commons-logging-1.1.2 – RRM Apr 16 '14 at 14:55
1

For observing file/dir changes using JDK 6, you could use:

WatchService for Java 6

For JDK 7, WatchService is part of NIO package:

http://java.dzone.com/news/how-watch-file-system-changes

Community
  • 1
  • 1
RRM
  • 2,495
  • 29
  • 46
  • I think this only watching for local file system , with file on remote host like : \\10.0.0.111/test/test.txt ,i can not found solution for that – Ryo Apr 14 '14 at 10:05
  • Did you try the solution for JDK 6 using Apache VFS? That is meant for distributed/network file system. – RRM Apr 14 '14 at 10:14
  • A simple and raw solution using NtlmPasswordAuthentication and SmbFile is that you collect lastModified and 'create time' for each SmbFile at appropriate frequency and store in map. So, a file entry which is not present in that map means a new file/dir was added and a different modified time would indicate an update. – RRM Apr 14 '14 at 10:26
1

Finally, this one works with JDK6 as well. This way, we could observe file/dir changes in windows shared drivers without mounting/mapping them as a drive.

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, jackrabbit-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();
    }
}
RRM
  • 2,495
  • 29
  • 46
  • The minimum required jars are: commons-vfs2-2.0, jcifs-1.3.17, commons-logging-1.1.2 – RRM Apr 16 '14 at 14:57