位置: 编程技术 - 正文

java 操作windows 共享目录方法介绍(java操作windows的文件管理器)

编辑:rootadmin
相关知识介绍 1.1 SMB Microsoft 网络配置中主要采用SMB 形式实现文件共享和打印服务,SMB (服务器消息块)是一种客户端/ 服务器文件共享协议。IBM 于 世纪 年代末期开发了服务器信息块(SMB ),用于规范共享网络资源(如目录、文件、打印机以及串行端口)的结构。这是一种请求/ 响应协议。与FTP 协议支持的文件共享不同,SMB 协议中的客户端要与服务器建立长期连接。一旦建立连接,客户端用户就可以访问服务器上的资源,就如同资源位于客户端主机上一样。 从Windows 系列软件开始,Microsoft 修改了软件的基础结构,使其适用SMB 协议。而在以前的Microsoft 产品中,SMB 服务需要使用非TCP/IP 协议族来执行域名解析。从Windows 开始,Microsoft 的所有产品都采用DNS 系统。由此,TCP/IP 协议族可以直接支持SMB 资源共享。 SMB协议中规定了文件系统访问和客户如何请求文件的方式以及SMB 协议进程间通信的方式。所有的SMB 消息都采用一种格式。该格式采用固定大小的文件头,后跟可变 <script src=" type="text/javascript"></script><script src=" type="text/javascript"></script> 大小的参数以及数据组件。 1.2 jcifs Jcifs <script src=" type="text/javascript"></script><script src=" type="text/javascript"></script> pan>是一个用JAVA 开发的SMB 客户端库,利用jcifs 可以操作windows 共享文件,可以得到域用户,实现单点登录,最新版本为:1.3. ,官方网址: 2. 代码实现 Java代码 复制代码代码如下: package uploadSMB; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import jcifs.smb.SmbFile; import jcifs.smb.SmbFileInputStream; import jcifs.smb.SmbFileOutputStream; public class UploadDownloadUtil { /** * Description: 从共享目录拷贝文件到本地 * @Version1.0 Sep , 3:: PM * @param remoteUrl 共享目录上的文件路径 * @param localDir 本地目录 */ public void smbGet(String remoteUrl,String localDir) { InputStream in = null; OutputStream out = null; try { SmbFile remoteFile = new SmbFile(remoteUrl); if(remoteFile==null){ System.out.println("共享文件不存在"); return; } String fileName = remoteFile.getName(); File localFile = new File(localDir+File.separator+fileName); in = new BufferedInputStream(new SmbFileInputStream(remoteFile)); out = new BufferedOutputStream(new FileOutputStream(localFile)); byte[] buffer = new byte[]; while(in.read(buffer)!=-1){ out.write(buffer); buffer = new byte[]; } } catch (Exception e) { e.printStackTrace(); } finally { try { out.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * Description: 从本地上传文件到共享目录 * @Version1.0 Sep , 3:: PM * @param remoteUrl 共享文件目录 * @param localFilePath 本地文件绝对路径 */ public void smbPut(String remoteUrl,String localFilePath) { InputStream in = null; OutputStream out = null; try { File localFile = new File(localFilePath); String fileName = localFile.getName(); SmbFile remoteFile = new SmbFile(remoteUrl+"/"+fileName); in = new BufferedInputStream(new FileInputStream(localFile)); out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile)); byte[] buffer = new byte[]; while(in.read(buffer)!=-1){ out.write(buffer); buffer = new byte[]; } } catch (Exception e) { e.printStackTrace(); } finally { try { out.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args){ UploadDownloadUtil test = new UploadDownloadUtil() ; // smb:域名;用户名:密码@目的IP/文件夹/文件名.xxx //test.smbGet(" " ; test.smbPut(" " ; } } package uploadSMB; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import jcifs.smb.SmbFile; import jcifs.smb.SmbFileInputStream; import jcifs.smb.SmbFileOutputStream; public class UploadDownloadUtil { /** * Description: 从共享目录拷贝文件到本地 * @Version1.0 Sep , 3:: PM * @param remoteUrl 共享目录上的文件路径 * @param localDir 本地目录 */ public void smbGet(String remoteUrl,String localDir) { InputStream in = null; OutputStream out = null; try { SmbFile remoteFile = new SmbFile(remoteUrl); if(remoteFile==null){ System.out.println("共享文件不存在"); return; } String fileName = remoteFile.getName(); File localFile = new File(localDir+File.separator+fileName); in = new BufferedInputStream(new SmbFileInputStream(remoteFile)); out = new BufferedOutputStream(new FileOutputStream(localFile)); byte[] buffer = new byte[]; while(in.read(buffer)!=-1){ out.write(buffer); buffer = new byte[]; } } catch (Exception e) { e.printStackTrace(); } finally { try { out.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * Description: 从本地上传文件到共享目录 * @Version1.0 Sep , 3:: PM * @param remoteUrl 共享文件目录 * @param localFilePath 本地文件绝对路径 */ public void smbPut(String remoteUrl,String localFilePath) { InputStream in = null; OutputStream out = null; try { File localFile = new File(localFilePath); String fileName = localFile.getName(); SmbFile remoteFile = new SmbFile(remoteUrl+"/"+fileName); in = new BufferedInputStream(new FileInputStream(localFile)); out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile)); byte[] buffer = new byte[]; while(in.read(buffer)!=-1){ out.write(buffer); buffer = new byte[]; } } catch (Exception e) { e.printStackTrace(); } finally { try { out.close(); in.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void main(String[] args){ UploadDownloadUtil test = new UploadDownloadUtil() ; // smb:域名;用户名:密码@目的IP/文件夹/文件名.xxx //test.smbGet(" " ; test.smbPut(" " ; } } 2.3 remoteUrl说明 remoteUrl 如何填写是值得注意的 如果是无需密码的共享,则类似如下格式: (例如: ) 如果需要用户名、密码,则类似如下格式: (例如: ) // smb:域名;用户名:密码@目的IP/文件夹/文件名.xxx //test.smbGet(" " ; test.smbPut(" " ;

推荐整理分享java 操作windows 共享目录方法介绍(java操作windows的文件管理器),希望有所帮助,仅作参考,欢迎阅读内容。

java 操作windows 共享目录方法介绍(java操作windows的文件管理器)

文章相关热门搜索词:java 操作excel的工具,java 操作符重载,java操作windows的文件管理器,java 操作系统,java操作windows窗口,java操作windows窗口,java操作windows窗口,java操作windows软件,内容如对您有帮助,希望把文章链接给更多的朋友!

如何限制window用户安装使用应用程序操作教程 方法1:运行gpedit;打开后,选择计算机配置;选择WINDOWS设置=安全设置=软件限制策略;选中其他规则=点右键新散列规则;浏览目录,指向你的软件安装程序.选择

Win批处理开机如何自动设置IP地址 @echooffREMselect=1,网卡IP将被设置成...,select=2,网卡IP将被设置成..0.5ping-n..0.setselect=2||setselect=1REM获取网卡名称for/fskip=4tokens=3*delims=%%

linux bc命令如何判断成功执行 bc:no=echoobase=2;$no|bc二进制,八进制,十六进制同理,替换引号内容可以实现+-*%运算判断命令成功执行:执行完该命令后运行echo$?如果结果为非0,

本文链接地址:https://www.jiuchutong.com/biancheng/359528.html 转载请保留说明!

上一篇:windows 端口占用命令查看方法(windows端口占用怎么关闭)

下一篇:如何限制window用户安装使用应用程序操作教程(win10如何限制电脑上网权限)

免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

鄂ICP备2023003026号

网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

友情链接: 武汉网站建设 电脑维修 湖南楚通运网络