Microsoft網絡配置中主要采用SMB形式實現文件共享和打印服務
從Windows
SMB協議中規定了文件系統訪問和客戶如何請求文件的方式以及SMB協議進程間通信的方式
Jcifs <script src=
package uploadSMB;
import java
import java
import java
import java
import java
import java
import java
import java
import jcifs
import jcifs
import jcifs
public class UploadDownloadUtil {
/**
* Description: 從共享目錄拷貝文件到本地
* @Version
* @param remoteUrl 共享目錄上的文件路徑
* @param localDir 本地目錄
*/
public void smbGet(String remoteUrl
InputStream in = null;
OutputStream out = null;
try {
SmbFile remoteFile = new SmbFile(remoteUrl);
if(remoteFile==null){
System
return;
}
String fileName = remoteFile
File localFile = new File(localDir+File
in = new BufferedInputStream(new SmbFileInputStream(remoteFile));
out = new BufferedOutputStream(new FileOutputStream(localFile));
byte[] buffer = new byte[
while(in
out
buffer = new byte[
}
} catch (Exception e) {
e
} finally {
try {
out
in
} catch (IOException e) {
e
}
}
}
/**
* Description: 從本地上傳文件到共享目錄
* @Version
* @param remoteUrl 共享文件目錄
* @param localFilePath 本地文件絕對路徑
*/
public void smbPut(String remoteUrl
InputStream in = null;
OutputStream out = null;
try {
File localFile = new File(localFilePath);
String fileName = localFile
SmbFile remoteFile = new SmbFile(remoteUrl+
in = new BufferedInputStream(new FileInputStream(localFile));
out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile));
byte[] buffer = new byte[
while(in
out
buffer = new byte[
}
} catch (Exception e) {
e
} finally {
try {
out
in
} catch (IOException e) {
e
}
}
}
public static void main(String[] args){
UploadDownloadUtil test = new UploadDownloadUtil() ;
// smb:域名;用戶名:密碼@目的IP/文件夾/文件名
//test
test
}
}
remoteUrl如何填寫是值得注意的
如果是無需密碼的共享
smb://ip/sharefolder(例如
如果需要用戶名
Smb://username:password@ip/sharefolder(例如
// smb:域名;用戶名:密碼@目的IP/文件夾/文件名
//test
test
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26941.html