前段时间做的一个项目有用到SMB协议的,通过局域网访问共享文件夹,关于SMB协议网上很多讲解,这里我不做过多解释,我只要讲下我在使用SMB协议访问远程服务器共享文件的时候的一些经验:
smb访问的基本格式:smb:域名;用户名:密码@目的IP/文件夹/文件名.xxx
注意:如果密码中有像“@”这种特殊字符的情况,就要通过
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("192.168.1.103", "Administrator", "19921103");
就行登录验证,具体实现如下:
package com.szitrus.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import org.apache.commons.io.IOUtils;
public class SMBUtils {
/**
* Description: 从共享目录拷贝文件到本地
*
* @Version1.0 Sep 25, 2009 3:48:38 PM
* @param remoteUrl
* 共享目录上的文件路径
* @param localDir
* 本地目录
*/
public static byte[] smbGet(String remoteUrl, NtlmPasswordAuthentication auth) {
byte[] bytes = {};
InputStream is = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
try {
SmbFile remoteFile = new SmbFile(remoteUrl, auth);
if (remoteFile == null) {
System.out.println("共享文件不存在");
return null;
}
is = remoteFile.getInputStream();
IOUtils.copy(is, baos);
bytes = baos.toByteArray();
} catch (Exception e) {
logger.error("文件从共享目录下载失败", e);
} finally {
if (null != baos){
try {
baos.close();
} catch (IOException e) {
logger.error("文件从共享目录下载失败", e);
}
}
if (null != is){
try {
is.close();
} catch (IOException e) {
logger.error("文件从共享目录下载失败", e);
}
}
}
return bytes;
}
/**
* Description: 从本地上传文件到共享目录
*
* @Version1.0 Sep 25, 2009 3:49:00 PM
* @param remoteUrl
* 共享文件目录
* @param localFilePath
* 本地文件绝对路径
*/
public static String smbPut(String remoteUrl, String localFilePath, NtlmPasswordAuthentication auth) {
String result = null;
FileInputStream fis = null;
try {
File localFile = new File(localFilePath);
localFile.setReadOnly();
String fileName = localFile.getName();
SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName, auth);
fis = new FileInputStream(localFile);
IOUtils.copyLarge(fis, remoteFile.getOutputStream());
result = "success";
} catch (Exception e) {
result = "failed";
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException e) {
logger.error("文件从上传失败", e);
}
}
return result;
}
/**
* Description: 从共享目录下载文件
*
* @Version1.0 Sep 25, 2009 3:48:38 PM
* @param remoteUrl
* 共享目录上的文件路径
*/
public static void smbDel(String remoteUrl, NtlmPasswordAuthentication auth) {
try {
SmbFile remoteFile = new SmbFile(remoteUrl, auth);
if (remoteFile.exists()) {
remoteFile.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SMBUtils test = new SMBUtils();
// smb:域名;用户名:密码@目的IP/文件夹/文件名.xxx
// test.smbGet("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake/test.txt",
// "c://") ;
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("192.168.1.103", "Administrator", "19921103"); // 先登录验证
// SmbFile fp = new SmbFile(remoteurl+"//"+dir,auth);
// test.smbPut("smb://10.12.91.156/smp_shared", "C:\\contract1659.pdf", auth);
// test.smbGet("smb://10.12.91.156/smp_shared/contractvayiVyjK.pdf",
// "C://asd.pdf", auth);
String url = "smb://192.168.1.103/shared/12.txt";
test.smbDel(url, auth);
}
}
希望对大家有所帮助。以下是涉及到的主要的jar包: