借助jCIFS 协议,jCIFS是CIFS在JAVA中的一个实现,是samba组织本着linux的精神,负责维护开发的一个开源项目。这个项目专注于使用java语言对 cifs协议的设计和实现。他们将jcifs设计成为一个完整的,丰富的,具有可扩展能力且线程安全的客户端库。这一库可以应用于各种java虚拟机访问 遵循CIFS/SMB网络传输协议的网络资源。
CIFS 使用客户/服务器模式。客户程序请求远在服务器上的服务器程序为它提供服务。服务器获得请求并返回响应。CIFS是公共的或开放的SMB协议版本,并由Microsoft使用。SMB协议(见最后的名词解释)现在是局域网上用于服务器文件访问和打印的协议。象SMB协议一样,CIFS在高层运行,而不象TCP/IP协议那样运行在底层。CIFS可以看做是应用程序协议如文件传输协议和超文本传输协议的一个实现。
jcfs 官网: http://jcifs.samba.org/
官网例子: http://jcifs.samba.org/src/docs/pipes.html
简单例子:
/**
* 从本地上传文件到共享目录
*
* @Version1.0 Sep 25, 2009 3:49:00 PM
* @param remoteUrl
* 共享文件目录
* @param localFilePath
* 本地文件绝对路径
*/
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) throws MalformedURLException {
String localPath="E:\\FinalJar\\commons-io-2.4-bin\\commons-io-2.4\\commons-io-2.4-tests.jar";
String netUrl="smb://Administrator:aaa@192.168.0.0.1/abc";
smbPut(netUrl, localPath);
}