Java 使用JCIFS访问网络文件共享的工具类
发布时间:2020-05-24 15:47:42 所属栏目:Java 来源:互联网
导读:Java 使用JCIFS访问网络文件共享的工具类
|
下面是脚本之家 jb51.cc 通过网络收集整理的代码片段。 脚本之家小编现在分享给大家,也给大家做个参考。 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:[emailprotected]/Jake/test.txt",// "c://") ;
// test.smbPut("smb://szpcg;jiang.t:[emailprotected]/Jake",// "c://test.txt");
//用户名密码不能有强字符,也就是不能有特殊字符,否则会被作为分断处理
test.smbGet("smb://CHINA;xieruilin:[emailprotected]/project/report/网上问题智能分析助手使用文档.doc","c://Temp/");
}
}
以上是脚本之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。 如果觉得脚本之家网站内容还不错,欢迎将脚本之家网站推荐给程序员好友。 (编辑:安卓应用网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
