使用凭据将 I/O 文件写入共享网络驱动器

2024-03-04

我想将 .txt 文件放到共享网络驱动器上。该路径是网络驱动器上的映射,需要凭据(登录名和密码)。我可以使用 FileOutputStream 传递这些参数吗?

FileOutputStream fos;
DataOutputStream dos;

try {
    File file= new File(path + "/" + fileName + ".txt");
    fos = new FileOutputStream(file);
    dos=new DataOutputStream(fos);
    dos.writeChars(stringContent);
    dos.close();
    fos.close();
}
catch(IOException eio){
}

谢谢。


不,使用javaCIFS 客户端库 http://jcifs.samba.org/。您可以通过java连接远程Windows机器。例子 -

String user = "user:password";
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(user);
String path = "smb://my_machine_name/D/MyDev/test.txt";
SmbFile sFile = new SmbFile(path, auth);
SmbFileOutputStream sfos = new SmbFileOutputStream(sFile);
sfos.write("Test".getBytes());
sfos.close();

Thanks

EDIT:JCIFS 仅支持不安全的 SMB1 协议,并且已处于维护模式多年。使用 jcifs-ng https://github.com/AgNO3/jcifs-ng用于 Windows 10 所需的 SMB2/SMB3 支持。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用凭据将 I/O 文件写入共享网络驱动器 的相关文章

随机推荐