Java 用JCIFS访问网络文件共享


public class UploadDownloadUtil
{

/**
* 从共享目录拷贝文件到本地
* @param remoteUrl 共享目录上的文件路径
* @param localDir 本地目录
*/
public void smbGet(String remoteUrl, String localDir)
{
InputStream in = null;
OutputStream out = null;
try
{
SmbFile remoteFile = new SmbFile(remoteUrl);
//这一句很重要
remoteFile.connect();
if (remoteFile == null)
{
System.out.println("共享文件不存在");
return;
}
String fileName = remoteFile.getName();
File localFile = new File(localDir + File.separator + fileName);
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];
}
}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
try
{
out.close();
in.close();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}

/**
* 从本地上传文件到共享目录
* @Version1.0 Sep 25, 2009 3:49:00 PM
* @param remoteUrl 共享文件目录
* @param localFilePath 本地文件绝对路径
*/
public 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)
{
UploadDownloadUtil test = new UploadDownloadUtil();
// smb:域名;用户名:密码@目的IP/文件夹/文件名.xxx
// test.smbGet("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake/test.txt",
// "c://") ;

// test.smbPut("smb://szpcg;jiang.t:xxx@192.168.193.13/Jake",
// "c://test.txt");


//用户名密码不能有强字符,也就是不能有特殊字符,否则会被作为分断处理
test.smbGet("smb://CHINA;xieruilin:123456Xrl@10.70.36.121/project/report/网上问题智能分析助手使用文档.doc",
"c://Temp/");

}

}


需要导入包jcifs-1.3.15.jar
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值