JCIFS简介and利用JCIFS网络文件共享实现上传下载

来源:互联网 发布:linux和windows传文件 编辑:程序博客网 时间:2024/06/06 01:40

JCIFS简介and利用JCIFS网络文件共享实现上传下载

JCIFS is an Open Source client library that implements the CIFS/SMB networking protocol in 100% Java. CIFS is the standard file sharing protocol on the Microsoft Windows platform (e.g. Map Network Drive ...). This client is used extensively in production on large Intranets.

JCIFS是一个开源实现100%的JavaCIFS / SMB网络协议的客户端库。CIFSMicrosoft Windows平台(如映射网络驱动器)上的标准的文件共享协议。此客户端被广泛应用于生产大型企业网。

 

JCIFS开源项目官网:https://jcifs.samba.org/

 

例子:

导入jcifs-1.1.11.jar 到项目中就可以使用了。

前提:远程主机要把对应的文件夹设置为共享,并知道主机网络地址和密码。

package test;

 

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 testjcfs {

/**

 * 从局域网中共享文件中得到文件并保存在本地磁盘上

 * @param remoteUrl 共享电脑路径 如:smb//administrator:123456@172.16.10.136/smb/1221.zip  , smb为共享文件

 * 注:如果一直出现连接不上,有提示报错,并且错误信息是 用户名活密码错误 则修改共享机器的文件夹选项 查看 去掉共享简单文件夹的对勾即可。

 * @param localDir 本地路径 如:D:/

 */

public static void smbGet(String remoteUrl,String localDir){

InputStream in = null;

OutputStream out = null;

try {

SmbFile smbFile = new SmbFile(remoteUrl);

String fileName = smbFile.getName();

File localFile = new File(localDir+File.separator+fileName);

in = new BufferedInputStream(new SmbFileInputStream(smbFile));

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();

}

}

}

/**

 * 把本地磁盘中的文件上传到局域网共享文件下

 * @param remoteUrl 共享电脑路径 如:smb//administrator:123456@172.16.10.136/smb

 * @param localFilePath 本地路径 如:D:/

 */

public static 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) {

// smbPut("smb://administrator:123456@172.16.10.136/smb", "E:/1221.zip");

smbGet("smb://administrator:123456@172.16.10.136/share/nia/456/你好.txt", "D:/中文/");

 

}

}

 

在网页上实现下载的例子:

 

request.setCharacterEncoding("UTF-8");

//   String fileaddr = request.getParameter("fileaddr");

   String fileaddr = "\\189\\files\\nia\\新建 Microsoft Word 文档.doc";

  

   response.reset();

   response.setCharacterEncoding("UTF-8");

    response.addHeader("content-type","application/x-msdownload");

  

   int index=fileaddr.lastIndexOf("\\");

    String filename=fileaddr.substring(index+1);

    String filedisplay = filename;

    filedisplay = URLEncoder.encode(filedisplay,"UTF-8");

    response.addHeader("Content-Disposition","attachment;filename=" + filedisplay);

    

    String remotepath = fileaddr.substring(5);

   String smbstr = "smb://administrator:123456@172.16.10.136/files/" + remotepath;

  

   String remoteUrl = smbstr;

//   String localDir = null;

InputStream in = null;

OutputStream ou = null;

try {

SmbFile smbFile = new SmbFile(remoteUrl);

String fileName = smbFile.getName();

// File localFile = new File(localDir+File.separator+fileName);

in = new BufferedInputStream(new SmbFileInputStream(smbFile));

ou = response.getOutputStream();

// ou = new BufferedOutputStream(new FileOutputStream(localFile));

byte []buffer = new byte[1024];

while((in.read(buffer)) != -1){

ou.write(buffer);

buffer = new byte[1024];

}

ou.flush();

catch (Exception e) {

System.out.println("04Error!"+e.getMessage());

// e.printStackTrace();

}finally{

try {

ou.close();

in.close();

out.clear();

out = pageContext.pushBody();

catch (IOException e) {

e.printStackTrace();

}

}

 

CIFS (Common Internet File System) 

通用Internet文件系统

  在windows主机之间进行网络文件共享是通过使用微软公司自己的CIFS服务实现的。 

  CIFS 是一个新提出的协议,它使程序可以访问远程Internet计算机上的文件并要求此计算机的服务。CIFS 使用客户/服务器模式。客户程序请求远在服务器上的服务器程序为它提供服务。服务器获得请求并返回响应。CIFS是公共的或开放的SMB协议版本,并由Microsoft使用。SMB协议(见最后的名词解释)现在是局域网上用于服务器文件访问和打印的协议。象SMB协议一样,CIFS在高层运行,而不象TCP/IP协议那样运行在底层。CIFS可以看做是应用程序协议如文件传输协议和超文本传输协议的一个实现。

  CIFS 可以使您达到以下功能:

  1.访问服务器本地文件并读写这些文件

  2.与其它用户一起共享一些文件块

  3.在断线时自动恢复与网络的连接

  4.使用西欧字符文件名

  一般来说,CIFS使用户得到比FTP更好的对文件的控制。它提供潜在的更直接地服务器程序接口,这比使用HTTP协议的浏览器更好。CIFS最典型的应用是windows用户能够从网上邻居中找到网络中的其他主机并访问其中的共享文件夹.

  CIFS 是开放的标准而且已经被作为Internet应用程序标准被提交到IETF

JCIFSJavaCIFS客户端库,即,JCIFSCIFS JAVA中的一个实现,是samba组织负责维护开发的一个开源项目,专注于使用java语言对cifs协议的设计和实现。他们将jcifs设计成为一个完整的,丰富的,具有可扩展能力且线程安全的客户端库。这一库可以应用于各种java虚拟机访问遵循CIFS/SMB网络传输协议的网络资源。类似于java.io.File的接口形式,在多线程的工作方式下被证明是有效而容易使用的。 

  JCIFS的开发方法类似java的文件操作功能,它的资源url定位:smb://{user}:{password}@{host}/{path}smb为协议名,userpassword分别为共享文件机子的登陆名和密码,@后面是要访问的资源的主机名或IP地址。最后是资源的共享文件夹名称和共享资源名。例如 smb://administrator:122122@192.168.0.22/test/response.txt 

  在JAVA程序中,使用如下方式获得一个远程共享文件的句柄:SmbFile file = new SmbFile("smb://guest:1234@192.168.3.56/share/a.txt");这里的句柄不仅限于远程的共享文件,还可能是共享文件夹。isFile()方法和isDirectory()用来判断这个句柄对应的资源的真实属性。如果是共享文件夹,通过调用它的list()方法将获得其中资源的列表。List方法支持过滤器机制,有两种过滤器可供使用,一种是SmbFileFilter,另一种是SmbFilenameFilter,这两个在jcifs中作为接口出现,你可以根据自己的需要派生出个性化的过滤器,实现接口中的accept方法,以满足不同业务的需求。 

  SmbFileInputStreamsmb文件的输入流,它的功能是以流的方式打开一个SmbFileSmbFileInputStream in = new SmbFileInputStream(file);SmbFileInputStream提供read方法,你可以从这个流中读出远程文件全部的内容。 

  jcifs可从http://jcifs.samba.org/下载,下面是一个简单的例子: 

  import jcifs.smb.SmbFileInputStream; 

  import jcifs.smb.SmbFile; 

  public class ReadShareFile { 

  public static void main(String[] args) { 

  try{ 

  SmbFile smbFile=new SmbFile("smb://test:test@10.218.100.12/share2/aa.txt"); 

  //通过 smbFile.isDirectory();isFile()可以判断smbFile是文件还是文件夹 

  int length=smbFile.getContentLength();//得到文件的大小 

  byte buffer[] = new byte[length] ; 

  SmbFileInputStream in = new SmbFileInputStream(smbFile) ; //建立smb文件输入流 

  while((in.read(buffer)) != -1){ 

  System.out.write(buffer); 

  System.out.println(buffer.length); 

  

  in.close(); 

  }catch(Exception e){ 

  e.printStackTrace(); 

  
  
  }

名词解释:

SMBServer Message Block:通信协议是微软(Microsoft)和英特尔(Intel)1987年制定的协议,主要是作为Microsoft网络的通讯协议。SMB 是在会话层(session layer)和表示层(presentation layer)以及小部分应用层(application layer)的协议。SMB使用了NetBIOS的应用程序接口 (Application Program Interface,简称API)。另外,它是一个开放性的协议,允许了协议扩展——使得它变得更大而且复杂;大约有65个最上层的作业,而每个作业都超过120个函数,甚至Windows NT也没有全部支持到,最近微软又把 SMB 改名为 CIFSCommon Internet  ile System),并且加入了许多新的特色。


以上内容多来自网上学习,整理

0 0
原创粉丝点击