zoukankan      html  css  js  c++  java
  • 在java程序中访问windows有用户名和密码保护的共享目录

    Java程序中访问拥有全部读写权限的目录相对比较简单,和普通的目录没有什么差别。
    但是要访问一个需要用户和密码验证的目录就需要一点点小技巧了。
    这里介绍一个开源的库能够比较容易的实现这一需求。
    1。 下载库文件:
     https://jcifs.samba.org/
    下载的zip文件中, 不仅包含了jar文件,还有文档和示例。

    2。拷贝jcif-1.3.18.jar到类路径中。

    3。代码示例:
     1     String user = "your_user_name";
     2     String pass ="your_pass_word";
     3 
     4     String sharedFolder="shared";
     5     String path="smb://ip_address/"+sharedFolder+"/test.txt";
     6     NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("",user, pass);
     7     SmbFile smbFile = new SmbFile(path,auth);
     8     SmbFileOutputStream smbfos = new SmbFileOutputStream(smbFile);
     9     smbfos.write("testing.and writing to a file".getBytes());
    10     System.out.println("completed nice !");
    说明: 如果有一个共享目录,比如: \192.168.1.2 estdir
    那么smb的路径就是:smb://192.168.1.2/testdir/
    NtlmPasswordAuthentication需要三个参数, 第一个是域名,没有的话,填null, 第二个是用户名,第三个是密码

    得到SmbFile之后,操作就和java.io.File基本一样了。
    另外还有一些功能比如:
    SmbFile.copyTo
    SmbFile.renameTo
    等等
  • 相关阅读:
    西瓜书的读书笔记
    Tensorflow-gpu在windows10上的安装(anaconda)
    php 设置报错等级
    PHP 类型比较表
    PHP中的定界符格式
    php进制转换函数
    php魔法常量
    php中双$$与多$$
    php引用传值
    CSS3过渡效果实现菜单划出效果
  • 原文地址:https://www.cnblogs.com/firstdream/p/5160899.html
Copyright © 2011-2022 走看看