NAS4Freeを使用して NAS サーバーをセットアップし、次のフォルダーを共有しました。
\\NAS_SERVER_IP/SHARE_FOLDER_NAME
SHARE_FOLDER_NAME ディレクトリには、複数のクライアントと共有する必要があるリソース ファイルが含まれています
クライアントから、共有フォルダをローカル クライアントにマウントせずに、Java を使用して NAS サーバーから直接ファイルにアクセス (読み取り/書き込み) できますか?
最後に、これはJDK6 でも動作します。このようにして、Windows共有ドライバーをドライブとしてマウント/マッピングせずに、ファイル/ディレクトリの変更を観察できました。
クラスパスで次の 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、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();
}
}
ここからコピーしましたが、API 呼び出しの引数を変更しました。
Java を使用して Windows の共有フォルダーに接続する
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());
}
JDK 6 を使用してファイル/ディレクトリの変更を監視するには、次を使用できます。
JDK 7 の場合、WatchService は NIO パッケージの一部です。