Windows で JCIFS を利用してみる

CIFS を Java から利用するためのライブラリ JCIFS を試してみたメモです.

JCIFS とは

JCIFS は Java で書かれた CIFS/SMB クライアントライブラリ.

File Read

code

[sourcecode language=”java” title=”” ]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Properties;

import jcifs.Config;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;

public class JCIFSSample {
public static void main (String[] args) throws IOException {

Properties prop = new Properties ();
prop.setProperty (“jcifs.smb.client.username”, “TSUNEMICHI”);
prop.setProperty (“jcifs.smb.client.password”, “****************”);
Config.setProperties (prop);

SmbFileInputStream sfis = new SmbFileInputStream (“smb://TSUNEMICHI-VAIO/Users/TSUNEMICHI/temp/test.txt”);
BufferedReader br = new BufferedReader (new InputStreamReader (sfis));

String line = null;

while ((line = br.readLine ())!=null){
System.out.println (line);
}
br.close ();
}
}
[/sourcecode]

compile and run

[sourcecode language=”text” title=”” ]
C:\cygwin64\home\tsu-nera\tmp>javac -cp .;jcifs-1.3.17.jar FileRead.java

C:\cygwin64\home\tsu-nera\tmp>java -cp .;jcifs-1.3.17.jar FileRead
Hello CIFS!!
[/sourcecode]

WireShark

[sourcecode language=”text” title=”” ]
Negotiate Protocol Request
Negotiate Protocol Response, ACCEPTOR_NEGO, ACCEPTOR_META_DATA
Session Setup AndX Request, NTLMSSP_NEGOTIATE
Session Setup AndX Response, NTLMSSP_CHALLENGE, Error: STATUS_MORE_PROCESSING_REQUIRED
Session Setup AndX Request, NTLMSSP_AUTH, User: ?\TSUNEMICHI
Session Setup AndX Response
Tree Connect AndX Request, Path: \\TSUNEMICHI-VAIO\USERS
Tree Connect AndX Response
NT Create AndX Request, FID: 0x4000, Path: \TSUNEMICHI\temp\test.txt
NT Create AndX Response, FID: 0x4000
Read AndX Request, FID: 0x4000, 4286 bytes at offset 0
Read AndX Response, FID: 0x4000, 12 bytes
Read AndX Request, FID: 0x4000, 4286 bytes at offset 12
Read AndX Response, FID: 0x4000, 0 bytes
Read AndX Request, FID: 0x4000, 4286 bytes at offset 12
Read AndX Response, FID: 0x4000, 0 bytes
Close Request, FID: 0x4000
Close Response, FID: 0x4000
[/sourcecode]

File Write

code

[sourcecode language=”java” title=”” ]
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Properties;

import jcifs.Config;
import jcifs.smb.SmbFileOutputStream;

public class FileWrite {
public static void main (String[] args) throws IOException {

Properties prop = new Properties ();
prop.setProperty (“jcifs.smb.client.username”, “TSUNEMICHI”);
prop.setProperty (“jcifs.smb.client.password”, “****************”);
Config.setProperties (prop);

SmbFileOutputStream sfos = new SmbFileOutputStream (“smb://TSUNEMICHI-VAIO/Users/TSUNEMICHI/temp/test.txt”);
PrintWriter pw = new PrintWriter (sfos);
pw.println (“GoodBye JCIFS”);
pw.close ();
}
}
[/sourcecode]

WireShark

[sourcecode language=”emacs-lisp” title=”” ]
Negotiate Protocol Request
Negotiate Protocol Response, ACCEPTOR_NEGO, ACCEPTOR_META_DATA
Session Setup AndX Request, NTLMSSP_NEGOTIATE
Session Setup AndX Response, NTLMSSP_CHALLENGE, Error: STATUS_MORE_PROCESSING_REQUIRED
Session Setup AndX Request, NTLMSSP_AUTH, User: ?\TSUNEMICHI
Session Setup AndX Response
Tree Connect AndX Request, Path: \\TSUNEMICHI-VAIO\USERS
Tree Connect AndX Response
NT Create AndX Request, FID: 0x4000, Path: \TSUNEMICHI\temp\test.txt
NT Create AndX Response, FID: 0x4000
Write AndX Request, FID: 0x4000, 15 bytes at offset 0
Write AndX Response, FID: 0x4000, 15 bytes
Close Request, FID: 0x4000
Close Response, FID: 0x4000
[/sourcecode]

その他

ローカルファイルとネットワーク上のファイルを 同じように扱う方法がわからなかった.

以下のような方法があるようだが, 自分の環境では動作せず.