01 Oct 2014, 12:05

Windows で JCIFS を利用してみる

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

JCIFS とは

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

File Read

code

<div class="outline-text-3" id="text-2-1">
  [sourcecode language=&#8221;java&#8221; title=&#8221;&#8221; ]<br /> import java.io.BufferedReader;<br /> import java.io.IOException;<br /> import java.io.InputStreamReader;<br /> import java.io.PrintWriter;<br /> import java.util.Properties;</p> 

  <p>
    import jcifs.Config;<br /> import jcifs.smb.SmbFileInputStream;<br /> import jcifs.smb.SmbFileOutputStream;
  </p>

  <p>
    public class JCIFSSample {<br /> public static void main (String[] args) throws IOException {
  </p>

  <p>
    Properties prop = new Properties ();<br /> prop.setProperty (&#8220;jcifs.smb.client.username&#8221;, &#8220;TSUNEMICHI&#8221;);<br /> prop.setProperty (&#8220;jcifs.smb.client.password&#8221;, &#8220;****************&#8221;);<br /> Config.setProperties (prop);
  </p>

  <p>
    SmbFileInputStream sfis = new SmbFileInputStream (&#8220;smb://TSUNEMICHI-VAIO/Users/TSUNEMICHI/temp/test.txt&#8221;);<br /> BufferedReader br = new BufferedReader (new InputStreamReader (sfis));
  </p>

  <p>
    String line = null;
  </p>

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

compile and run

<div class="outline-text-3" id="text-2-2">
  [sourcecode language=&#8221;text&#8221; title=&#8221;&#8221; ]<br /> C:\cygwin64\home\tsu-nera\tmp>javac -cp .;jcifs-1.3.17.jar FileRead.java</p> 

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

WireShark

<div class="outline-text-3" id="text-2-3">
  [sourcecode language=&#8221;text&#8221; title=&#8221;&#8221; ]<br /> Negotiate Protocol Request<br /> Negotiate Protocol Response, ACCEPTOR_NEGO, ACCEPTOR_META_DATA<br /> Session Setup AndX Request, NTLMSSP_NEGOTIATE<br /> Session Setup AndX Response, NTLMSSP_CHALLENGE, Error: STATUS_MORE_PROCESSING_REQUIRED<br /> Session Setup AndX Request, NTLMSSP_AUTH, User: ?\TSUNEMICHI<br /> Session Setup AndX Response<br /> Tree Connect AndX Request, Path: \\TSUNEMICHI-VAIO\USERS<br /> Tree Connect AndX Response<br /> NT Create AndX Request, FID: 0x4000, Path: \TSUNEMICHI\temp\test.txt<br /> NT Create AndX Response, FID: 0x4000<br /> Read AndX Request, FID: 0x4000, 4286 bytes at offset 0<br /> Read AndX Response, FID: 0x4000, 12 bytes<br /> Read AndX Request, FID: 0x4000, 4286 bytes at offset 12<br /> Read AndX Response, FID: 0x4000, 0 bytes<br /> Read AndX Request, FID: 0x4000, 4286 bytes at offset 12<br /> Read AndX Response, FID: 0x4000, 0 bytes<br /> Close Request, FID: 0x4000<br /> Close Response, FID: 0x4000<br /> [/sourcecode]
</div>

File Write

code

<div class="outline-text-3" id="text-3-1">
  [sourcecode language=&#8221;java&#8221; title=&#8221;&#8221; ]<br /> import java.io.BufferedReader;<br /> import java.io.IOException;<br /> import java.io.InputStreamReader;<br /> import java.io.PrintWriter;<br /> import java.util.Properties;</p> 

  <p>
    import jcifs.Config;<br /> import jcifs.smb.SmbFileOutputStream;
  </p>

  <p>
    public class FileWrite {<br /> public static void main (String[] args) throws IOException {
  </p>

  <p>
    Properties prop = new Properties ();<br /> prop.setProperty (&#8220;jcifs.smb.client.username&#8221;, &#8220;TSUNEMICHI&#8221;);<br /> prop.setProperty (&#8220;jcifs.smb.client.password&#8221;, &#8220;****************&#8221;);<br /> Config.setProperties (prop);
  </p>

  <p>
    SmbFileOutputStream sfos = new SmbFileOutputStream (&#8220;smb://TSUNEMICHI-VAIO/Users/TSUNEMICHI/temp/test.txt&#8221;);<br /> PrintWriter pw = new PrintWriter (sfos);<br /> pw.println (&#8220;GoodBye JCIFS&#8221;);<br /> pw.close ();<br /> }<br /> }<br /> [/sourcecode]
  </p>
</div>

WireShark

<div class="outline-text-3" id="text-3-2">
  [sourcecode language=&#8221;emacs-lisp&#8221; title=&#8221;&#8221; ]<br /> Negotiate Protocol Request<br /> Negotiate Protocol Response, ACCEPTOR_NEGO, ACCEPTOR_META_DATA<br /> Session Setup AndX Request, NTLMSSP_NEGOTIATE<br /> Session Setup AndX Response, NTLMSSP_CHALLENGE, Error: STATUS_MORE_PROCESSING_REQUIRED<br /> Session Setup AndX Request, NTLMSSP_AUTH, User: ?\TSUNEMICHI<br /> Session Setup AndX Response<br /> Tree Connect AndX Request, Path: \\TSUNEMICHI-VAIO\USERS<br /> Tree Connect AndX Response<br /> NT Create AndX Request, FID: 0x4000, Path: \TSUNEMICHI\temp\test.txt<br /> NT Create AndX Response, FID: 0x4000<br /> Write AndX Request, FID: 0x4000, 15 bytes at offset 0<br /> Write AndX Response, FID: 0x4000, 15 bytes<br /> Close Request, FID: 0x4000<br /> Close Response, FID: 0x4000<br /> [/sourcecode]
</div>

その他

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

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