jcifs java_Java 使用JCIFS访问网络文件共享的工具类
導(dǎo)讀熱詞
下面是編程之家 jb51.cc 通過網(wǎng)絡(luò)收集整理的代碼片段。
編程之家小編現(xiàn)在分享給大家,也給大家做個參考。
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/網(wǎng)上問題智能分析助手使用文檔.doc","c://Temp/");
}
}
以上是編程之家(jb51.cc)為你收集整理的全部代碼內(nèi)容,希望文章能夠幫你解決所遇到的程序開發(fā)問題。
如果覺得編程之家網(wǎng)站內(nèi)容還不錯,歡迎將編程之家網(wǎng)站推薦給程序員好友。
相關(guān)文章
總結(jié)
如果覺得編程之家網(wǎng)站內(nèi)容還不錯,歡迎將編程之家網(wǎng)站推薦給程序員好友。
本圖文內(nèi)容來源于網(wǎng)友網(wǎng)絡(luò)收集整理提供,作為學(xué)習(xí)參考使用,版權(quán)屬于原作者。
如您喜歡交流學(xué)習(xí)經(jīng)驗,點擊鏈接加入交流1群:1065694478(已滿)交流2群:163560250
總結(jié)
以上是生活随笔為你收集整理的jcifs java_Java 使用JCIFS访问网络文件共享的工具类的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中gettext文件格式_P
- 下一篇: tokudb mysql_【MySQL】