您的位置:首页 > 其它

利用SMB协议操作共享文件夹

2016-05-12 12:09 656 查看

利用SMB协议操作共享文件夹

SMB:服务器信息块,是一个网络文件共享协议。举一简单例子,在一个局域网内,有两台机器A,B,假设A共享了一个文件,那么可以在B上就可以使用SBM协议来操作这个文件夹。假如A机器有一个用户test,密码与是test,而共享的文件夹名称为电影,机器A的IP是192.168.0.102.代码如下:


import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;

import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;

public class Main {

private static String url = "smb://test:test@192.168.0.102/电影/";
private static String moveUrl = "smb://test:test@192.168.0.102/电影/tt.dll";
private static String targetUrl = "smb://test:test@192.168.0.102/电影/newdir/";

/**创建目录*/
public static void createDir(SmbFile parent)
{
try{
if(parent != null && parent.isDirectory())
{
String path = parent.getPath() + "newdir/";
SmbFile newFile = new SmbFile(path);
if(newFile == null || !newFile.exists())
{
newFile.mkdir();
}
}
}catch(Exception e)
{
e.printStackTrace();
}
}

/**上传文件到服务器*/
public static void uploadFile(File file)
{
BufferedInputStream bf = null;
SmbFileOutputStream smbOut = null;
try{
smbOut = new SmbFileOutputStream(url + "/" + file.getName(), false);
bf = new BufferedInputStream(new FileInputStream(file));
byte[] bt = new byte[8192];
int n = bf.read(bt);
while (n != -1){
smbOut.write(bt, 0, n);
smbOut.flush();
n = bf.read(bt);
}
}catch(Exception e) {
e.printStackTrace();
}finally{
try {
if(null != smbOut)
smbOut.close();
if(null != bf)
bf.close();
}catch(Exception e2) {
e2.printStackTrace();
}
}
}

/**移动文件到另一个目录*/
public static void moveFile()
{
SmbFileOutputStream smbOut = null;
BufferedInputStream bf = null;
SmbFile moveFile = null;
boolean flag = false;
try{
moveFile = new SmbFile(moveUrl);
if(moveFile != null)
{
String name = moveFile.getName();
smbOut = new SmbFileOutputStream(targetUrl + name, false);
bf = new BufferedInputStream(new SmbFileInputStream(moveFile));
byte[] bt = new byte[8192];
int n = bf.read(bt);
while (n != -1){
smbOut.write(bt, 0, n);
smbOut.flush();
n = bf.read(bt);
}
}

flag = true;
}catch(Exception e)
{
e.printStackTrace();
}finally{
try {
if(null != smbOut)
smbOut.close();
if(null != bf)
bf.close();
if(moveFile != null && flag)
moveFile.delete();
}catch(Exception e2) {
e2.printStackTrace();
}
}
}

/**复制文件到另一个目录*/
public static void copyFile()
{
SmbFileOutputStream smbOut = null;
BufferedInputStream bf = null;
SmbFile moveFile = null;
try{
moveFile = new SmbFile(moveUrl);
if(moveFile != null)
{
String name = moveFile.getName();
smbOut = new SmbFileOutputStream(targetUrl + name, false);
bf = new BufferedInputStream(new SmbFileInputStream(moveFile));
byte[] bt = new byte[8192];
int n = bf.read(bt);
while (n != -1){
smbOut.write(bt, 0, n);
smbOut.flush();
n = bf.read(bt);
}
}

}catch(Exception e)
{
e.printStackTrace();
}finally{
try {
if(null != smbOut)
smbOut.close();
if(null != bf)
bf.close();
}catch(Exception e2) {
e2.printStackTrace();
}
}
}

/**文件重命名*/
public static void rename()
{
try{
SmbFile moveFile = new SmbFile(moveUrl);
SmbFile targetFile = new SmbFile(moveFile.getParent() + "111111.dll/");
moveFile.renameTo(targetFile);
}catch(Exception e)
{
e.printStackTrace();
}

}

public static void main(String[] args)
{
try{
SmbFile smbFile = new SmbFile(url);
if(smbFile.isDirectory())
{
//创建一个文件夹
//              createDir(smbFile);

//上传一个文件
//              uploadFile(new File("F://test//tt.dll"));

//移动一个文件到另一目录
//              moveFile();

//复制一个文件到另一个目录
//              copyFile();

//重命名一个文件
rename();

SmbFile[] list = smbFile.listFiles();
int length = list.length;
for(int i = 0;i < length;i ++)
{
System.out.println(list[i].getName() + " " + list[i].getParent());
}
}
}catch(Exception e)
{
e.printStackTrace();
}
}
}


在这里我们将会使用到的一个额外的jar包,为jcifs-1.1.11.jar。该jar可以由网上下载

下载地址
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  SMB 共享文件 共享