差異處

這裏顯示兩個版本的差異處。

連向這個比對檢視

Both sides previous revision 前次修改
下次修改
前次修改
java:jcifs [2015/01/04 01:38]
tony [How to?]
java:jcifs [2023/06/25 09:48] (目前版本)
行 4: 行 4:
 JCIFS是java-based存取網路芳鄰通訊協定的API。\\ JCIFS是java-based存取網路芳鄰通訊協定的API。\\
 \\ \\
-之所以接觸這個API,是因為好奇Windows管理工具[[http://​blog.xuite.net/​jyoutw/​xtech/​24607577-PsTools+%E4%B9%8B+PsExec+%E7%9A%84%E7%94%A8%E6%B3%95|psexec]],能將Local程式放到Remote並執行的做法。後來看到類似工具[[http://​www.poweradmin.com/​paexec/​|paexec]]程式碼,與經過自己的測試,發現原理是將程式複製到遠端的[[http://​en.wikipedia.org/​wiki/​Administrative_share|管理共享資料夾]],再透過WMI呼叫外部程式。\\+之所以接觸這個API,是因為好奇Windows管理工具[[http://​blog.xuite.net/​jyoutw/​xtech/​24607577-PsTools+%E4%B9%8B+PsExec+%E7%9A%84%E7%94%A8%E6%B3%95|psexec]],能將Local程式放到Remote並執行的做法。後來看到類似工具[[http://​www.poweradmin.com/​paexec/​|paexec]]程式碼,與經過自己的測試,發現原理是將程式複製到遠端的[[http://​en.wikipedia.org/​wiki/​Administrative_share|管理共享資料夾]],再透過WMI呼叫外部程式。\\
 \\ \\
 接下來將說明我有使用的API。 接下來將說明我有使用的API。
行 22: 行 22:
  return new SmbFile(aTarget,​ auth);  return new SmbFile(aTarget,​ auth);
  }  }
 +</​code>​
 +如果有網域,可以使用
 +<code java>
 +new NtlmPasswordAuthentication(domain,​ user, passwd);
 </​code>​ </​code>​
 在建立完SmbFile物件後,剩下其實就和操作一般檔案無異。 在建立完SmbFile物件後,剩下其實就和操作一般檔案無異。
行 108: 行 112:
  }  }
 </​code>​ </​code>​
 +===== Test =====
 +簡單的寫了對檔案建立與複製的單元測試,這是對某台Win7機器的管理共享做存取。
 +<code java>
 +public class SmbFileUtilTest {
 +
 + private String targetFolder = "​smb://​192.168.1.25/​admin$/";​
 + private String user = "​administrator";​
 + private String passwd = "​123456";​
 +
 + private String remoteTmpFile = null;
 + private String localTmpFile = null;
 +
 + @After
 + public void tearDown() throws IOException{
 + if( remoteTmpFile != null ){
 + SmbFileUtil.deleteFile(user,​ passwd, remoteTmpFile);​
 + assertFalse(SmbFileUtil.exists(user,​ passwd, remoteTmpFile));​
 + }
 + if( localTmpFile != null ){
 + new File(localTmpFile).delete();​
 + }
 + }
 +
 + @Test
 + public void copyFile() throws IOException{
 + remoteTmpFile = targetFolder+"​temp.txt";​
 + localTmpFile = "​temp.tmp";​
 + SmbFileUtil.copyFileTo(user,​ passwd, "​libs/​jcifs-1.3.18.jar",​ remoteTmpFile);​
 + assertTrue(SmbFileUtil.exists(user,​ passwd, remoteTmpFile));​
 +
 + SmbFileUtil.copyFileFrom(user,​ passwd, remoteTmpFile, ​ localTmpFile);​
 + File localFile = new File(localTmpFile);​
 + assertTrue(localFile.exists());​
 +
 + assertEquals(new File("​libs/​jcifs-1.3.18.jar"​).length(),​ localFile.length());​
 + }
 +
 + @Test
 + public void createFile() throws IOException{
 + remoteTmpFile = targetFolder+"​temp.txt";​
 + SmbFileUtil.createFile(user,​ passwd , remoteTmpFile,​ "​test"​);​
 + assertTrue(SmbFileUtil.exists(user,​ passwd, remoteTmpFile));​
 + }
 +}
 +</​code>​
 +===== Summary =====
 +要將檔案複製到遠端,也可以透過
 +  - 在本地建立網芳或Samba Server,讓遠端由本地複製檔案。
 +  - 使用echo在遠端建立wget的VB Script,從本地的http server去wget檔案。
 +不管是以上兩種方法,或是透過管理共享,都是有可能因為使用者權限與安全性設定而失敗。只能見招拆招了。
 ===== Reference ===== ===== Reference =====
   * [[http://​jcifs.samba.org/​|JCIFS official site]]   * [[http://​jcifs.samba.org/​|JCIFS official site]]
   * [[http://​sanjaal.com/​java/​875/​java-utilities/​java-tutorial-using-jcifs-to-copy-files-to-shared-network-drive-using-username-and-password/​|Copy File]]   * [[http://​sanjaal.com/​java/​875/​java-utilities/​java-tutorial-using-jcifs-to-copy-files-to-shared-network-drive-using-username-and-password/​|Copy File]]
 +  * [[http://​stackoverflow.com/​questions/​14749434/​how-to-copy-file-from-smb-share-to-local-drive-not-in-domain-with-jcifs|JCIFS with domain]]
 +
 +=====    =====
 +----
 +\\
 +~~DISQUS~~