在Java程序中读写windows共享文件夹
摘要 使用Java通过JCIFS框架读写共享文件夹,使用SMB协议,并支持域认证。
项目常常需要有访问共享文件夹的需求,例如读取共享文件夹存储的视频、照片和PPT等文件。那么如何使用Java读写Windows共享文件夹呢?
使用Java访问拥有全部读写权限的共享文件夹比较简单,和普通的目录没有什么区别。但是,如果从Linux服务器访问一个windows服务器上需要用户名和密码验证的共享文件夹,就需要一点点小技巧了。这里介绍一个开源的库JCIFS,他可以轻松满足此需求。
JCIFS是使用Java语言开发的一款开源框架,通过SMB协议访问远程共享文件夹,就像访问本地文件夹一样,简简单单。她同时支持Windows和Linux共享文件夹,不过,Linux共享文件夹需要安装Samba服务软件(官网:http://www.samba.org/)。
另外,通过挂载该共享文件夹到Linux服务器下也可以实现,此时不需要借助SMB协议,对此,这里不做介绍,感兴趣的童鞋可以去问问度娘。
言归正传,maven依赖如下:
<!-- https://mvnrepository.com/artifact/org.samba.jcifs/jcifs -->
<dependency>
<groupId>org.samba.jcifs</groupId>
<artifactId>jcifs</artifactId>
<version>1.3.3</version>
</dependency>
SMB(Server Message Block)通信协议是微软(Microsoft)和英特尔(Intel)在1987年制定的协议,主要是作为Microsoft网络的通讯协议。SMB 是在会话层(session layer)和表示层(presentation layer)以及小部分应用层(application layer)的协议。通过设置“NetBIOS over TCP/IP”使得Samba不但能与局域网络主机分享资源,还能与全世界的电脑分享资源。
代码示例:
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 org.slf4j.Logger;
import org.slf4j.LoggerFactory; import jcifs.smb.NtlmPasswordAuthentication;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream; public class SmbFileUtil { private static Logger log = LoggerFactory.getLogger(SmbFileUtil.class); private static String USER_DOMAIN = "yourDomain";
private static String USER_ACCOUNT = "yourAccount";
private static String USER_PWS = "yourPassword"; public static void main(String[] args) throws Exception { // remoteUrl 格式示例 【smb://132.20.2.33/CIMPublicTest/】
// 目录
String shareDir = "smb:// 132.20.2.33/CIMPublicTest/";
listFiles(shareDir); } /**
* @Title listFiles
* @Description 遍历指定目录下的文件
* @date 2019-01-11 09:56
*/
private static void listFiles(String shareDirectory) throws Exception {
long startTime = System.currentTimeMillis(); // 域服务器验证
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(USER_DOMAIN, USER_ACCOUNT,
USER_PWS);
SmbFile remoteFile = new SmbFile(shareDirectory, auth);
log.info("远程共享目录访问耗时:【{}】", System.currentTimeMillis() - startTime); if (remoteFile.exists()) {
SmbFile[] files = remoteFile.listFiles();
remoteFile.listFiles(shareDirectory); for (SmbFile f : files) {
log.info(f.getName());
}
} else {
log.info("文件不存在");
}
}
}
NtlmPasswordAuthentication类用于域认证,需要三个参数, 第一个是域名,如果没有,就赋值null, 第二个是用户名,第三个是密码。下面列举SMB协议的两个应用场景。SmbFile类和Java的File类十分相似,使用其对象可以处理远程共享文件的读写。
/**
* @Title smbGet
* @Param shareUrl 共享目录中的文件路径,如smb://132.20.2.33/CIMPublicTest/eg.txt
* @Param localDirectory 本地目录,如tempStore/smb
*/
public static void smbGet(String shareUrl, String localDirectory) throws Exception {
NtlmPasswordAuthentication auth = new NtlmPasswordAuthentication(USER_DOMAIN, USER_ACCOUNT,
USER_PWS);
SmbFile remoteFile = new SmbFile(shareUrl, auth);
if (!remoteFile.exists()) {
log.info("共享文件不存在");
return;
}
// 有文件的时候再初始化输入输出流
InputStream in = null;
OutputStream out = null;
log.info("下载共享目录的文件 shareUrl 到 localDirectory");
try {
String fileName = remoteFile.getName();
File localFile = new File(localDirectory + File.separator + fileName);
File fileParent = localFile.getParentFile();
if (null != fileParent && !fileParent.exists()) {
fileParent.mkdirs();
}
in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
out = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] buffer = new byte[1024];
while (in.read(buffer) != -1) {
out.write(buffer);
buffer = new byte[1024];
}
out.flush(); //刷新缓冲区输出流 } catch (Exception e) {
e.printStackTrace();
} finally {
out.close();
in.close();
}
} /**
*
* @Title smbPut
* @Description 向共享目录上传文件
* @Param shareDirectory 共享目录
* @Param localFilePath 本地目录中的文件路径
* @date 2019-01-10 20:16
*/
public static void smbPut(String shareDirectory, String localFilePath) {
InputStream in = null;
OutputStream out = null;
try {
File localFile = new File(localFilePath); String fileName = localFile.getName();
SmbFile remoteFile = new SmbFile(shareDirectory + "/" + 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];
}
out.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
在Java程序中读写windows共享文件夹的更多相关文章
- Java读写Windows共享文件夹 .
版权声明:本文为博主原创文章,未经博主允许不得转载. 项目常常需要有访问共享文件夹的需求,例如共享文件夹存储照片.文件等.那么如何使用Java读写Windows共享文件夹呢? Java可以使用JCIF ...
- 在java程序中访问windows有用户名和密码保护的共享目录
在java程序中访问windows有用户名和密码保护的共享目录 Posted on 2015-11-20 14:03 云自无心水自闲 阅读(3744) 评论(0) 编辑 收藏 --> Jav ...
- CentOS中设置Windows共享文件夹
在CentOS中设置Samba可实现和Windows共享文件夹.常见的需求:1)用户能够在Windows机器上通过共享文件夹访问远程Linux服务器上自己的主目录:2)用户能够在Windows机器上访 ...
- Mac OS访问Windows共享文件夹
原文地址:http://blog.csdn.net/jinhill/article/details/7246922 最近开始研究Mac OS,遇到的第一个问题就是如何在Mac OS中访问Windows ...
- 烂泥:CentOS6.5挂载windows共享文件夹
本文由秀依林枫提供友情赞助,首发于烂泥行天下. 由于工作需要,需要把本机的文件夹共享出去,然后让CentOS服务器临时使用下. 服务器使用的是CentOS系统,而本机使用的win7系统.考虑到是临时使 ...
- linux上挂载windows共享文件夹
linux上挂载windows共享文件夹 1.共享windows目录 挂载之前得创建一个有password的用户(当前用户也能够),并将你要挂载的目录进行共享,并赋予读写权限 如图. watermar ...
- windows共享文件夹如何让CentOS 6.5读取
http://www.111cn.net/sys/CentOS/74104.htm 工作需要,需要把本地win7共享的文件夹让CenotOS 6.5服务器临时使用一下,以下是CentOS 6.5系统挂 ...
- 在Linux下访问Windows共享文件夹
说明 以下操作以Ubuntu为例,大家可以参考. 我在Ubuntu 14.04和16.04都试过了. Windows共享文件夹 如果局域网内有一台Windows主机,将指定文件夹设为共享,就可以在局域 ...
- Linux上从Java程序中调用C函数
原则上来说,"100%纯Java"的解决方法是最好的,但有些情况下必须使用本地方法.特别是在以下三种情况: 需要访问Java平台无法访问的系统特性和设备: 通过基准测试,发现Jav ...
随机推荐
- Yii2手动下载PHPExcel引入
步奏一 下载PHPExcel (自行下载:下载地址http://phpexcel.codeplex.com/releases/view/119187) 步骤二 将PHPExcel解压后的PHPEx ...
- 正确理解springboot的常用注入方式
springboot的属性注入 以注入dataSource为例1.springboot默认读取的文件是放在resources目录下的名为application.properties或applicati ...
- ABBYY FineReader 12如何识别包含非常规符号的文本
ABBYY FineReader 12 是一款OCR图文识别软件,可快速方便地将扫描纸质文档.PDF文件和数码相机的图像转换成可编辑.可搜索的文本,有时文本中可能会包含一些非常规的符号,此时ABBYY ...
- [AWS] User management
IAM用户管理 Ref: AWS系列-创建 IAM 用户 Ref: AWS系列:深入了解IAM和访问控制 是什么? IAM enables you to control who can do what ...
- Git工作流程最佳实践总结
Git作为一个目前非常流行的版本管理工具,深受开发者的喜爱.那么怎样才能将Git的作用发挥的更好呢?我根据实际的项目经验,归纳总结了以下Git工作流的最佳实践.这里所谓的最佳,是经过多次项目经验后,根 ...
- Http 调用netty 服务,服务调用客户端,伪同步响应.ProtoBuf 解决粘包,半包问题.
实际情况是: 公司需要开发一个接口给新产品使用,需求如下 1.有一款硬件设备,客户用usb接上电脑就可以,但是此设备功能比较单一,所以开发一个服务器程序,辅助此设备业务功能 2.解决方案,使用Sock ...
- 【python】并查集
转自:http://blog.csdn.net/rav009/article/details/12781899 # -*- coding: UTF-8 -*- class unionfind: def ...
- 关于linux下mysql 5.7.x数据库的yum的安装方法
环境介绍>>>>>>>>>>>>>>>>>> 操作系统:Centos 7.1 mysql数据 ...
- jenkins编辑报错Exception when publishing, exception message的解决办法
jenkins编辑报错Exception when publishing, exception message的解决办法 查看目标主机的磁盘空间是否占满,清理磁盘空间即可
- 部署不能产生class文件的问题
项目clean和重新部署项目之后,还是不能产生class文件:查看“Problem”视图,是lib路径有问题,右击项目→“Build Path”→“Configure Build Path”,Libr ...