同一個區域網路內,使用 java 從伺服器共用資料夾中複製檔案到本地。

來源:互聯網
上載者:User

標籤: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 從伺服器共用資料夾中複製檔案到本地。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.