SAMBA的JAVA客户端JCIFS

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了。。。汗
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值