- 浏览: 2486137 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
SAMBA的JAVA客户端JCIFS
首页
http://jcifs.samba.org/
下载文件
jcifs-1.2.25b.zip
下载的版本很新啊。能从MAVEN上搞到的版本如下pom.xml
<dependency>
<groupId>org.samba.jcifs</groupId>
<artifactId>jcifs</artifactId>
<version>1.2.15</version>
</dependency>
提供调用的static 工具类SambaUtil.java:
package com.sillycat.api.commons.utils;
import java.net.MalformedURLException;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
public class SambaUtil {
public static void delete(String filepath, String username, String pwd)
{
SmbFile f = null;
try {
f = new SmbFile("smb://" + username + ":" + pwd + "@"
+ filepath);
try {
if (f.exists()) {
f.delete();
}
} catch (SmbException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public static boolean exists(String filepath, String username, String pwd)
throws Exception {
SmbFile file = new SmbFile("smb://" + username + ":" + pwd + "@"
+ filepath);
try {
return file.exists();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static boolean filerename(String filepath, String newFilename,
String username, String pwd) {
try {
SmbFile f = new SmbFile("smb://" + username + ":" + pwd + "@"
+ filepath);
if (f.isFile()) {
String str = filepath.substring(0, filepath.lastIndexOf("/"));
str = "smb://" + username + ":" + pwd + "@" + str + "/"
+ newFilename;
f.renameTo(new SmbFile(str));
} else if (f.isDirectory()) {
String str = filepath.substring(0, filepath.length() - 1);
str = filepath.substring(0, str.lastIndexOf("/"));
str = "smb://" + username + ":" + pwd + "@" + str + "/"
+ newFilename;
f.renameTo(new SmbFile(str));
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static void mkdir(String dir, String username, String pwd) {
try {
SmbFile f = new SmbFile("smb://" + username + ":" + pwd + "@" + dir);
if (!f.exists()) {
f.mkdir();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void mkfile(String filepath, String username, String pwd) {
try {
SmbFile f = new SmbFile("smb://" + username + ":" + pwd + "@"
+ filepath);
if (!f.exists()) {
f.createNewFile();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void mkfile(String filepath, String username, String pwd,
String content) {
try {
SmbFile f = new SmbFile("smb://" + username + ":" + pwd + "@"
+ filepath);
if (!f.exists())
f.createNewFile();
writefile(filepath, content, username, pwd);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String readfile(String filepath, String username, String pwd) {
StringBuffer sb = new StringBuffer("");
try {
SmbFile f = new SmbFile("smb://" + username + ":" + pwd + "@"
+ filepath);
if (f.exists() && f.isFile()) {
int length = f.getContentLength();// 得到文件的大小
byte buffer[] = new byte[length];
SmbFileInputStream in = new SmbFileInputStream(f);
while ((in.read(buffer)) != -1) {
sb.append(new String(buffer));
}
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
public static boolean isdir(String filepath, String username, String pwd)
throws Exception {
String dir = "smb://" + username + ":" + pwd + "@" + filepath;
SmbFile f = new SmbFile(dir);
return f.isDirectory();
}
public static void writefile(String filepath, String content,
String username, String pwd) {
try {
SmbFile to = new SmbFile("smb://" + username + ":" + pwd + "@"
+ filepath);
SmbFileOutputStream out = new SmbFileOutputStream(to);
out.write(content.getBytes());
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
测试类SambaUtilTest.java:
package com.sillycat.api.commons.utils;
import junit.framework.TestCase;
public class SambaUtilTest extends TestCase {
protected void setUp() throws Exception {
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testDummy() {
assertTrue(true);
}
public void testCreateDir() {
String dir = "www.sillycat.com/share/test1";
SambaUtil.mkdir(dir, "guest", "guest");
SambaUtil.mkfile(dir + "/test2.txt", "guest", "guest", "<html></html>中文hello world!");
String tmp = SambaUtil.readfile(dir + "/test2.txt", "guest", "guest");
System.out.println(tmp);
SambaUtil.delete(dir + "/test2.txt", "guest", "guest");
}
}
报错:
jcifs.smb.SmbException: The network name cannot be found.
原来是我的filepath写错了,写成www.sillycat.com/test1
改成www.sillycat.com/share/test1就OK了
报错:
jcifs.smb.SmbException: The process cannot access the file because it is being used by another process.
原来是方法里面的
SmbFileInputStream in = new SmbFileInputStream(f);
SmbFileOutputStream out = new SmbFileOutputStream(to);
这个in 和 out 都要注意 close,我有一个忘记close了。。。汗
首页
http://jcifs.samba.org/
下载文件
jcifs-1.2.25b.zip
下载的版本很新啊。能从MAVEN上搞到的版本如下pom.xml
<dependency>
<groupId>org.samba.jcifs</groupId>
<artifactId>jcifs</artifactId>
<version>1.2.15</version>
</dependency>
提供调用的static 工具类SambaUtil.java:
package com.sillycat.api.commons.utils;
import java.net.MalformedURLException;
import jcifs.smb.SmbException;
import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import jcifs.smb.SmbFileOutputStream;
public class SambaUtil {
public static void delete(String filepath, String username, String pwd)
{
SmbFile f = null;
try {
f = new SmbFile("smb://" + username + ":" + pwd + "@"
+ filepath);
try {
if (f.exists()) {
f.delete();
}
} catch (SmbException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
public static boolean exists(String filepath, String username, String pwd)
throws Exception {
SmbFile file = new SmbFile("smb://" + username + ":" + pwd + "@"
+ filepath);
try {
return file.exists();
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static boolean filerename(String filepath, String newFilename,
String username, String pwd) {
try {
SmbFile f = new SmbFile("smb://" + username + ":" + pwd + "@"
+ filepath);
if (f.isFile()) {
String str = filepath.substring(0, filepath.lastIndexOf("/"));
str = "smb://" + username + ":" + pwd + "@" + str + "/"
+ newFilename;
f.renameTo(new SmbFile(str));
} else if (f.isDirectory()) {
String str = filepath.substring(0, filepath.length() - 1);
str = filepath.substring(0, str.lastIndexOf("/"));
str = "smb://" + username + ":" + pwd + "@" + str + "/"
+ newFilename;
f.renameTo(new SmbFile(str));
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
public static void mkdir(String dir, String username, String pwd) {
try {
SmbFile f = new SmbFile("smb://" + username + ":" + pwd + "@" + dir);
if (!f.exists()) {
f.mkdir();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void mkfile(String filepath, String username, String pwd) {
try {
SmbFile f = new SmbFile("smb://" + username + ":" + pwd + "@"
+ filepath);
if (!f.exists()) {
f.createNewFile();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void mkfile(String filepath, String username, String pwd,
String content) {
try {
SmbFile f = new SmbFile("smb://" + username + ":" + pwd + "@"
+ filepath);
if (!f.exists())
f.createNewFile();
writefile(filepath, content, username, pwd);
} catch (Exception e) {
e.printStackTrace();
}
}
public static String readfile(String filepath, String username, String pwd) {
StringBuffer sb = new StringBuffer("");
try {
SmbFile f = new SmbFile("smb://" + username + ":" + pwd + "@"
+ filepath);
if (f.exists() && f.isFile()) {
int length = f.getContentLength();// 得到文件的大小
byte buffer[] = new byte[length];
SmbFileInputStream in = new SmbFileInputStream(f);
while ((in.read(buffer)) != -1) {
sb.append(new String(buffer));
}
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return sb.toString();
}
public static boolean isdir(String filepath, String username, String pwd)
throws Exception {
String dir = "smb://" + username + ":" + pwd + "@" + filepath;
SmbFile f = new SmbFile(dir);
return f.isDirectory();
}
public static void writefile(String filepath, String content,
String username, String pwd) {
try {
SmbFile to = new SmbFile("smb://" + username + ":" + pwd + "@"
+ filepath);
SmbFileOutputStream out = new SmbFileOutputStream(to);
out.write(content.getBytes());
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
测试类SambaUtilTest.java:
package com.sillycat.api.commons.utils;
import junit.framework.TestCase;
public class SambaUtilTest extends TestCase {
protected void setUp() throws Exception {
super.setUp();
}
protected void tearDown() throws Exception {
super.tearDown();
}
public void testDummy() {
assertTrue(true);
}
public void testCreateDir() {
String dir = "www.sillycat.com/share/test1";
SambaUtil.mkdir(dir, "guest", "guest");
SambaUtil.mkfile(dir + "/test2.txt", "guest", "guest", "<html></html>中文hello world!");
String tmp = SambaUtil.readfile(dir + "/test2.txt", "guest", "guest");
System.out.println(tmp);
SambaUtil.delete(dir + "/test2.txt", "guest", "guest");
}
}
报错:
jcifs.smb.SmbException: The network name cannot be found.
原来是我的filepath写错了,写成www.sillycat.com/test1
改成www.sillycat.com/share/test1就OK了
报错:
jcifs.smb.SmbException: The process cannot access the file because it is being used by another process.
原来是方法里面的
SmbFileInputStream in = new SmbFileInputStream(f);
SmbFileOutputStream out = new SmbFileOutputStream(to);
这个in 和 out 都要注意 close,我有一个忘记close了。。。汗
发表评论
-
Update Site will come soon
2021-06-02 04:10 1609I am still keep notes my tech n ... -
Portainer 2020(4)Deploy Nginx and Others
2020-03-20 12:06 378Portainer 2020(4)Deploy Nginx a ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 373Private Registry 2020(1)No auth ... -
Docker Compose 2020(1)Installation and Basic
2020-03-15 08:10 325Docker Compose 2020(1)Installat ... -
VPN Server 2020(2)Docker on CentOS in Ubuntu
2020-03-02 08:04 397VPN Server 2020(2)Docker on Cen ... -
Nginx Deal with OPTIONS in HTTP Protocol
2020-02-15 01:33 299Nginx Deal with OPTIONS in HTTP ... -
PDF to HTML 2020(1)pdftohtml Linux tool or PDFBox
2020-01-29 07:37 342PDF to HTML 2020(1)pdftohtml Li ... -
Elasticsearch Cluster 2019(2)Kibana Issue or Upgrade
2020-01-12 03:25 592Elasticsearch Cluster 2019(2)Ki ... -
Spark Streaming 2020(1)Investigation
2020-01-08 07:19 229Spark Streaming 2020(1)Investig ... -
Hadoop Docker 2019 Version 3.2.1
2019-12-10 07:39 255Hadoop Docker 2019 Version 3.2. ... -
MongoDB 2019(3)Security and Auth
2019-11-16 06:48 202MongoDB 2019(3)Security and Aut ... -
MongoDB 2019(1)Install 4.2.1 Single and Cluster
2019-11-11 05:07 248MongoDB 2019(1) Follow this ht ... -
Monitor Tool 2019(1)Monit Installation and Usage
2019-10-17 08:22 283Monitor Tool 2019(1)Monit Insta ... -
Ansible 2019(1)Introduction and Installation on Ubuntu and CentOS
2019-10-12 06:15 269Ansible 2019(1)Introduction and ... -
Timezone and Time on All Servers and Docker Containers
2019-10-10 11:18 291Timezone and Time on All Server ... -
Kafka Cluster 2019(6) 3 Nodes Cluster on CentOS7
2019-10-05 23:28 237Kafka Cluster 2019(6) 3 Nodes C ... -
K8S Helm(1)Understand YAML and Kubectl Pod and Deployment
2019-10-01 01:21 287K8S Helm(1)Understand YAML and ... -
Rancher and k8s 2019(5)Private Registry
2019-09-27 03:25 321Rancher and k8s 2019(5)Private ... -
Jenkins 2019 Cluster(1)Version 2.194
2019-09-12 02:53 401Jenkins 2019 Cluster(1)Version ... -
Redis Cluster 2019(3)Redis Cluster on CentOS
2019-08-17 04:07 334Redis Cluster 2019(3)Redis Clus ...
相关推荐
jcifs-ext-0.9.4.jar/jcifs-1.3.3.jar 单点登陆 解决maven仓库有jar包但是maven程序无法下载仓库jar包
读取Samba文件使用 jcifs java 库从 Samba 读取文件。
代码直接可以用,包含完整的上传和下载代码,只需要修改ip和路径即可。
本文介绍了Linux Samba客户端工具Smbtree、nmblookup、smbclient、smbtar的使用方法。
Centos7.6 samba服务及客户端配置说明.txt
samba的安装与配置,并且在linux和windows不同环境下的访问方法介绍,以及可能通过LVM或者软RAID等工具创建大容量硬盘后,通过samba进行共享和访问
<groupId>org.samba.jcifs <artifactId>jcifs-ext <version>0.9.4 </dependency>
jcifs-1.3.19.jar 包,用于SMB协议 ,以实现远程读取文件。jcifs-1.3.19.jar 包,用于SMB协议 ,以实现远程读取文件。
linux 文件共享 samba 服务共享
Samba安装、客户端工具及服务器的配置.pptx
rhce253-Samba服务器及客户端的配置[汇编].pdf
Samba安装和配置教程 doc文档,实例说明,一看就懂
sambaforwindows-linux samba实现windows向linux备份文件部署实例.docx
删除Java 1.7之前的支持 统一身份验证子系统,NTLMSSP / Kerberos支持 大型ReadX / WriteX支持 流列表操作 NtTransNotifyChange支持 Google补丁:各种错误修复,lastAccess支持,重试请求 适当的测试套件 各种修复 ...
通过jcifs实现java访问网络共享文件 jcifs 是 cifs(common internet file system) java的实现 smb (service message block) to be cifs jcifs 是一个开源项目 可从http://jcifs.samba.org/下载 SmbFileInputStream...
jcifs能很好的实现集成登录,在企业中能够实现登录一次就可以访问多个系统
<groupId>org.samba.jcifs <artifactId>jcifs-ext <version>0.9.4 </dependency>
smbpasswd:该方式是使用smb工具smbpasswd给系统用户(真实用户或者虚拟用户)设置一个Samba 密码,客户端就用此密码访问Samba资源。smbpasswd在/etc/samba中,有时需要手工创建该文件。 tdbsam:使用数据库文件创建...
演示了如何访问共享文件,基于samba.
1. 利用rpm命令安装软件包的方法; 2. 安装、配置Samba服务器并启动; 3. 从Linux访问Windows资源; 4. 从Windows访问Linux的资源。