Java 使用JCIFS访问网络文件共享的工具类
发布时间:2020-12-15 00:15:47 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 public class UploadDownloadUtil{/** * 从共享目录拷贝文件到本地 * @param remoteUrl 共享目录上的文件路径 * @param localDir 本地目录 */public v
|
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 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:[email?protected]/Jake/test.txt",// "c://") ;
// test.smbPut("smb://szpcg;jiang.t:[email?protected]/Jake",// "c://test.txt");
//用户名密码不能有强字符,也就是不能有特殊字符,否则会被作为分断处理
test.smbGet("smb://CHINA;xieruilin:[email?protected]/project/report/网上问题智能分析助手使用文档.doc","c://Temp/");
}
}
以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
