共享文件下载--SMB

来源:互联网 发布:淘宝客服常见刁蛮问题 编辑:程序博客网 时间:2024/05/19 05:01

import jcifs.smb.SmbFile;
import jcifs.smb.SmbFileInputStream;
import java.io.*;

/**
 * need seven parameters
 * account is used to access share folder
 * password is used to access share folder
 * ip specifies host in which share folder locates
 * shareDir is the path of share folder
 * file specifies the file supposed to be downloaded.
 * localDir specifies the local directory for preserving files downloaded.
 * localFile specifies the file name for downloaded files.
 */
public class ShareFileDownload {
   
    public static void main(String[] args) {
       
        if(args.length == 7){
           
            String account = args[0].trim();
            String password = args[1].trim() ;
            String ip = args[2].trim();
            String shareDir = args[3].trim() ;
            String file = args[4].trim() ;
            String localDir = args[5].trim();
            String localFile = args[6].trim();
           
            try {
                String connStr = "smb://" + account + ":" + password + "@" + ip + "/" + shareDir + "/" + file ;
                System.out.println(connStr);
                SmbFile smbFile = new SmbFile(connStr);
                //"smb://pz:pz521@111.111.111.111/share/1.txt");
               
                int length = 1024*1024;
   
                SmbFileInputStream in = new SmbFileInputStream(smbFile);
                BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(localDir + "/" + localFile));

                byte[] buffer = new byte[length];
                while ((in.read(buffer)) != -1) {
   
                    out.write(buffer);
                    buffer = new byte[length];
                }
               
                in.close();
                out.close();
               
            } catch (Exception e) {
               
                e.printStackTrace();
            }
        }else{
           
            System.out.println("Need seven parameters.");
        }
       
    }
}

原创粉丝点击