java從局域網共享目錄下載文件

java從局域網共享目錄下載文件

直接貼代碼

 public static void main(String[] args) throws MalformedURLException {
        //smb://xxx:xxx@.../testIndex/
        //xxx:xxx是共享機器的用戶名密碼
        //SmbFile smbFile = new SmbFile("smb://域名;登錄賬號:登錄密碼@服務器ip/文件夾名稱/");
        String url="smb://Administrator:[email protected]/video/tt.mp4";
        SmbFile file = new SmbFile(url);
        System.out.println(file);
        getFile(url, "D:/log");
    }
 /**
     * 從共享目錄下載文件
     * @param remoteUrl 共享目錄
     * @param localDir 本地目錄
     */
    public static void getFile(String remoteUrl,String localDir) {
        InputStream in = null;
        OutputStream out = null;
        try {
            SmbFile remoteFile = new SmbFile(remoteUrl);
            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));
            System.out.println(in);
            out = new BufferedOutputStream(new FileOutputStream(localFile));
            System.out.println(remoteFile.toString());
            System.out.println(localFile.toString());
            System.out.println(in.toString());
            System.out.println(out.toString());
            byte[] buffer = new byte[2048];
            while(in.read(buffer)!=-1){
                out.write(buffer);
                buffer = new byte[2048];
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                assert out != null;
                out.close();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章