windows系统之间SMB共享文件夹的访问和操作
JCIFS使用示例工具 jcifs.jar获取途径下载地址前提背景:A系统与B系统的集成,A提交相关数据到B系统,数据格式为XML,XML标签中放着物理文件的访问路径,因A系统不能对外开发共享目录,B系统提供可访问操作的共享路径。
JCIFS使用
API中提供了实例说明,慢慢玩。
import jcifs.smb.*;jcifs.Config.setProperty( "bios.wins", "192.168.1.220" );NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication("domain", "username", "password");SmbFileInputStream in = new SmbFileInputStream("smb://host/c/My Documents/somefile.txt", auth);byte[] b = new byte[8192];int n;while(( n = in.read( b )) > 0 ) {System.out.write( b, 0, n );}
示例
这里采用了设置相关参数的用法,因为共享机器中用户密码包含@这样不能直接拼接path url。
区别点,从共享那里取文件SmbFileInputStream,向共享那里送文件SmbFileOutputStream
import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.List;import jcifs.Config;import jcifs.smb.NtlmPasswordAuthentication;import jcifs.smb.SmbException;import jcifs.smb.SmbFile;import jcifs.smb.SmbFileInputStream;import jcifs.smb.SmbFileOutputStream;public class SMBUtil {static NtlmPasswordAuthentication auth;static {//先祭天,如果不加,访问时长8秒起步,加快访问共享目录的速度Config.setProperty("bios.wins", "共享目录IP(0.0.0.0)");Config.setProperty("bios.domain", "共享目录所在域");Config.setProperty("bios.username", "共享目录用户名");Config.setProperty("bios.password", "共享目录用户密码");Config.setProperty("jcifs.smb.client.dfs.disabled", "true");System.setProperty("jcifs.smb.client.dfs.disabled", "true");auth = new NtlmPasswordAuthentication("域名", "用户名", "密码");}/*** 从共享目录下获取文件* @param sharedPathURL* @param auth*/public static void smbGet(String sharedPathURL, NtlmPasswordAuthentication auth) {// 共享路径需要处理一下:形如 smb://10.10.10.10/aString url = sharedPathURL.replaceAll("\\\\", "/");SmbFileInputStream fis = null;FileOutputStream fos = null;try {SmbFile smbFile = new SmbFile(url, auth);if (smbFile.isFile()) {fis = new SmbFileInputStream(smbFile);fos = new FileOutputStream(new File(smbFile.getName()));byte[] buff = new byte[1024*1024];int len;while((len = fis.read(buff))!=-1) {fos.write(buff, 0, len);}fos.flush();}else {SmbFile[] listFiles = smbFile.listFiles();for (SmbFile file : listFiles) {fis = new SmbFileInputStream(file);fos = new FileOutputStream(new File(file.getPath()));//这里需要定义文件存储的本地路径byte[] buff = new byte[1024*1024];int len;while((len = fis.read(buff))!=-1) {fos.write(buff, 0, len);}fos.flush();}}} catch (Exception e) {e.printStackTrace();}finally {if (fos != null) {try {fos.close();} catch (IOException e) {e.printStackTrace();}}if (fis != null) {try {fis.close();} catch (IOException e) {e.printStackTrace();}}}}/*** 将本地物理文件上传至共享目录中* @param sharedPathURL 共享目录的访问地址,类似:\\10.10.10.10\a* @param files需要上传到共享目录的本地文件列表* @param timetamp在时间戳在共享目录中创建文件夹用来区分记录* @param auth访问共享目录的认证* @return文件上传到共享目录之后,需要重新生成路径写入xml中,方便对方访问共享目录中的文件*/public static List<String> smbPut(String sharedPathURL, List<File> files, String timetamp, NtlmPasswordAuthentication auth) {// 共享路径需要处理一下:形如 smb://10.10.10.10/aString url = sharedPathURL.replaceAll("\\\\", "/");List<String> lists = new ArrayList<String>();FileInputStream fis = null;SmbFileOutputStream fos =null;SmbFile folder = null;if (null == files || files.size() == 0) {return null;}try {for (File file : files) {String filename = file.getName();//创建文件夹folder = new SmbFile(url+"/"+timetamp+"/", auth);if(!folder.exists()) folder.mkdirs();//创建文件SmbFile smbFile = new SmbFile(folder, filename);//正常的IO文件复制fis = new FileInputStream(file);fos = new SmbFileOutputStream(smbFile);byte[] buff = new byte[1024*1024];int len = 0;while((len=fis.read(buff))!=-1) {fos.write(buff, 0, len);}fos.flush();}} catch (Exception e) {e.printStackTrace();}finally {if (fos != null) {try {fos.close();} catch (IOException e) {e.printStackTrace();}}if (fis != null) {try {fis.close();} catch (IOException e) {e.printStackTrace();}}}try {SmbFile[] smbFiles = folder.listFiles();if (null != smbFiles && smbFiles.length > 0) {for (SmbFile smbFile : smbFiles) {String path = smbFile.getPath();lists.add(path.split(":")[1].replaceAll("/", "\\\\"));}}} catch (SmbException e) {e.printStackTrace();}return lists;}}
如果觉得《windows系统之间SMB共享文件夹的访问和操作》对你有帮助,请点赞、收藏,并留下你的观点哦!