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

JCIFS とは

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

File Read

code

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 ();
  }
}

compile and run

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!!

WireShark

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

File Write

code

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 ();
  }
}

WireShark

 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

その他

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

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