標籤:host options use pid put 讀取 ati sam 使用者名稱
1 引用jar 包
<dependency> <groupId>org.samba.jcifs</groupId> <artifactId>jcifs</artifactId> <version>1.3.14-kohsuke-1</version></dependency>
2 從本地上傳檔案到伺服器共用資料夾
import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.InputStream;import java.io.OutputStream;import jcifs.smb.SmbFile;import jcifs.smb.SmbFileOutputStream;/** * * <b>類名稱:</b>CopyFileToLan<br/> * <b>類描述:</b> 本地檔案寫入區域網路共用資料夾 <br/> * <b>版本:</b>V1.0<br/> */public class CopyFileToLan { public static void main(String[] args){ InputStream in = null; OutputStream out = null; try { //測試檔案 File localFile = new File("D:\\文檔\\test.txt"); String host = "192.168.1.151";//遠程伺服器的地址 String username = "admin";//使用者名稱 String password = "admin";//密碼 String path = "/share/";//遠程伺服器共用資料夾名稱 String remoteUrl = "smb://" + username + ":" + password + "@" + host + path + (path.endsWith("/") ? "" : "/"); SmbFile remoteFile = new SmbFile(remoteUrl + "/" + localFile.getName()); remoteFile.connect(); in = new BufferedInputStream(new FileInputStream(localFile)); out = new BufferedOutputStream(new SmbFileOutputStream(remoteFile)); byte[] buffer = new byte[4096]; int len = 0; while ((len = in.read(buffer, 0, buffer.length)) != -1) { out.write(buffer, 0, len); } out.flush(); } catch (Exception e) { String msg = "發生錯誤:" + e.getLocalizedMessage(); System.out.println(msg); } finally { try { if(out != null) { out.close(); } if(in != null) { in.close(); } } catch (Exception e) {} } }}3 從伺服器共用資料夾下載檔案到本地
import jcifs.smb.SmbFile;import jcifs.smb.SmbFileInputStream;import java.io.*;/** * <b>類名稱:</b>CopyLanFileToLocal<br/> * <b>類描述:</b> 讀取區域網路共用資料夾檔案,到本地檔案夾 <br/> */public class CopyLanFileToLocal { public static void main(String[] args) { InputStream in = null; OutputStream out = null; try { //目標檔案名 String fileName = "1.jpg"; //本地檔案 String localPath = "d:/"; String host = "192.168.1.151";//遠程伺服器的地址 String username = "admin";//使用者名稱 String password = "admin";//密碼 String path = "/share/";//遠程伺服器共用資料夾名稱 String remoteUrl = "smb://" + username + ":" + password + "@" + host + path + (path.endsWith("/") ? "" : "/"); //建立遠程檔案對象 SmbFile remoteFile = new SmbFile(remoteUrl + "/" + fileName); remoteFile.connect(); //建立檔案流 in = new BufferedInputStream(new SmbFileInputStream(remoteFile)); out = new BufferedOutputStream(new FileOutputStream(new File(localPath + fileName))); //讀取檔案內容 byte[] buffer = new byte[4096]; int len = 0; while ((len = in.read(buffer, 0, buffer.length)) != -1) { out.write(buffer, 0, len); } out.flush(); } catch (Exception e) { String msg = "下載遠程檔案出錯:" + e.getLocalizedMessage(); System.out.println(msg); } finally { try { if (out != null) { out.close(); } if (in != null) { in.close(); } } catch (Exception e) { } } }}
SmbFile 和 FIle 對檔案的操作基本一致, 在檔案複製的時候,想過用 commons-io 的FileUtils.copyFile(final File srcFile, final File destFile);和 java 1.7 的 Files.copy(Path source, Path target, CopyOption... options);但是由於SmbFile 和 File 不屬於一個類型,導致失敗,最後只能選擇使用流的方式進行操作。
同一個區域網路內,使用 java 從伺服器共用資料夾中複製檔案到本地。