黑松山资源网 Design By www.paidiu.com
相关知识介绍
1.1 SMB
Microsoft 网络配置中主要采用SMB 形式实现文件共享和打印服务,SMB (服务器消息块)是一种客户端/ 服务器文件共享协议。IBM 于20 世纪80 年代末期开发了服务器信息块(SMB ),用于规范共享网络资源(如目录、文件、打印机以及串行端口)的结构。这是一种请求/ 响应协议。与FTP 协议支持的文件共享不同,SMB 协议中的客户端要与服务器建立长期连接。一旦建立连接,客户端用户就可以访问服务器上的资源,就如同资源位于客户端主机上一样。
从Windows 2000 系列软件开始,Microsoft 修改了软件的基础结构,使其适用SMB 协议。而在以前的Microsoft 产品中,SMB 服务需要使用非TCP/IP 协议族来执行域名解析。从Windows 2000 开始,Microsoft 的所有产品都采用DNS 系统。由此,TCP/IP 协议族可以直接支持SMB 资源共享。
SMB协议中规定了文件系统访问和客户如何请求文件的方式以及SMB 协议进程间通信的方式。所有的SMB 消息都采用一种格式。该格式采用固定大小的文件头,后跟可变 <script src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" type="text/javascript"></script><script src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" type="text/javascript"></script> 大小的参数以及数据组件。
1.2 jcifs
Jcifs <script src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" type="text/javascript"></script><script src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" type="text/javascript"></script> pan>是一个用JAVA 开发的SMB 客户端库,利用jcifs 可以操作windows 共享文件,可以得到域用户,实现单点登录,最新版本为:1.3.12 ,官方网址:http://jcifs.samba.org/
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 25, 2009 3:48:38 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[1024];
while(in.read(buffer)!=-1){
out.write(buffer);
buffer = new byte[1024];
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Description: 从本地上传文件到共享目录
* @Version1.0 Sep 25, 2009 3:49:00 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[1024];
while(in.read(buffer)!=-1){
out.write(buffer);
buffer = new byte[1024];
}
} 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("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake/test.txt", "c://") ;
test.smbPut("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake", "c://test.txt") ;
}
}
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 25, 2009 3:48:38 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[1024];
while(in.read(buffer)!=-1){
out.write(buffer);
buffer = new byte[1024];
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Description: 从本地上传文件到共享目录
* @Version1.0 Sep 25, 2009 3:49:00 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[1024];
while(in.read(buffer)!=-1){
out.write(buffer);
buffer = new byte[1024];
}
} 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("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake/test.txt", "c://") ;
test.smbPut("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake", "c://test.txt") ;
}
}
2.3 remoteUrl说明
remoteUrl 如何填写是值得注意的
如果是无需密码的共享,则类似如下格式:
smb://ip/sharefolder (例如:smb://192.168.0.77/test )
如果需要用户名、密码,则类似如下格式:
Smb://username:password@ip/sharefolder (例如:smb://chb:123456@192.168.0.1/test )
// smb:域名;用户名:密码@目的IP/文件夹/文件名.xxx
//test.smbGet("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake/test.txt", "c://") ;
test.smbPut("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake", "c://test.txt") ;
1.1 SMB
Microsoft 网络配置中主要采用SMB 形式实现文件共享和打印服务,SMB (服务器消息块)是一种客户端/ 服务器文件共享协议。IBM 于20 世纪80 年代末期开发了服务器信息块(SMB ),用于规范共享网络资源(如目录、文件、打印机以及串行端口)的结构。这是一种请求/ 响应协议。与FTP 协议支持的文件共享不同,SMB 协议中的客户端要与服务器建立长期连接。一旦建立连接,客户端用户就可以访问服务器上的资源,就如同资源位于客户端主机上一样。
从Windows 2000 系列软件开始,Microsoft 修改了软件的基础结构,使其适用SMB 协议。而在以前的Microsoft 产品中,SMB 服务需要使用非TCP/IP 协议族来执行域名解析。从Windows 2000 开始,Microsoft 的所有产品都采用DNS 系统。由此,TCP/IP 协议族可以直接支持SMB 资源共享。
SMB协议中规定了文件系统访问和客户如何请求文件的方式以及SMB 协议进程间通信的方式。所有的SMB 消息都采用一种格式。该格式采用固定大小的文件头,后跟可变 <script src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" type="text/javascript"></script><script src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" type="text/javascript"></script> 大小的参数以及数据组件。
1.2 jcifs
Jcifs <script src="http://hi.images.csdn.net/js/blog/tiny_mce/themes/advanced/langs/zh.js" type="text/javascript"></script><script src="http://hi.images.csdn.net/js/blog/tiny_mce/plugins/syntaxhl/langs/zh.js" type="text/javascript"></script> pan>是一个用JAVA 开发的SMB 客户端库,利用jcifs 可以操作windows 共享文件,可以得到域用户,实现单点登录,最新版本为:1.3.12 ,官方网址:http://jcifs.samba.org/
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 25, 2009 3:48:38 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[1024];
while(in.read(buffer)!=-1){
out.write(buffer);
buffer = new byte[1024];
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Description: 从本地上传文件到共享目录
* @Version1.0 Sep 25, 2009 3:49:00 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[1024];
while(in.read(buffer)!=-1){
out.write(buffer);
buffer = new byte[1024];
}
} 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("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake/test.txt", "c://") ;
test.smbPut("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake", "c://test.txt") ;
}
}
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 25, 2009 3:48:38 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[1024];
while(in.read(buffer)!=-1){
out.write(buffer);
buffer = new byte[1024];
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* Description: 从本地上传文件到共享目录
* @Version1.0 Sep 25, 2009 3:49:00 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[1024];
while(in.read(buffer)!=-1){
out.write(buffer);
buffer = new byte[1024];
}
} 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("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake/test.txt", "c://") ;
test.smbPut("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake", "c://test.txt") ;
}
}
2.3 remoteUrl说明
remoteUrl 如何填写是值得注意的
如果是无需密码的共享,则类似如下格式:
smb://ip/sharefolder (例如:smb://192.168.0.77/test )
如果需要用户名、密码,则类似如下格式:
Smb://username:password@ip/sharefolder (例如:smb://chb:123456@192.168.0.1/test )
// smb:域名;用户名:密码@目的IP/文件夹/文件名.xxx
//test.smbGet("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake/test.txt", "c://") ;
test.smbPut("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake", "c://test.txt") ;
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
更新日志
2024年10月09日
2024年10月09日
- 《原神》「星路拾忆」网页活动壁纸展示
- 群星.2024-祝你幸福!电影原声带【阿里巴巴】【FLAC分轨】
- 于台烟.2004-执着【乾坤唱片】【WAV+CUE】
- 黄品源.2004-感谢·情人(引进版)【滚石】【WAV+CUE】
- 《最终幻想16》评测:游、戏两难全的系列转折
- 《纸嫁衣》五部打包¥104,《鬼泣5》仅需¥44
- 《崩坏:星穹铁道》造物大赛卷出新高度
- 《王菲 菲常传奇 2CD》[WAV+CUE][950MB]
- 《王菲 空灵独味唱腔 非常传奇 紫水晶2CD》[WAV+CUE][860MB]
- 《车载专业测试王1号 极品音质》[WAV/分轨][980MB]
- 炉石传说的国服回归送什么奖励 炉石传说9.25回归最新消息介绍
- 云顶之弈最强阵容汇总2024 云顶之弈最强阵容搭配最新版本
- S14lol全球总决赛在哪观看 lol全球总决赛观赛直播地址汇总
- 黄思婷2011-感谢天[豪记唱片][WAV+CUE]
- 孙淑媚2013-太阳[台湾首版][WAV+CUE]