Copy all files from server to Android Device

后端 未结 2 1331
北恋
北恋 2021-01-16 02:34

i want to copy all files from server to Android device. Suppose on server, my server ip is http://192.168.98.23 and the name of the server folder is

2条回答
  •  清酒与你
    2021-01-16 03:01

    As you can say you are using LAN to transfer files from server to Android (Sdcard). For this purpose there are two approaches you can use.i.e i) TCP/IP protocol. ii) SMB (Server Message Block) protocol. I recommend you to use SMB protocol because in this you have to just sharing a folder with full permissions and copy all the files to Android Sdcard. At Android side in this case which is your client side you have to use four things. i) IP Address of the server. ii) Password of the Server. iii) UserName of the server and the last iv) Shared FolderName. With the help of these four parameters you make a connection and copy all the files which is placed into the Shared Folder.

    Follow the code snippet that is used to make a connection using smb protocole.

     public boolean VerifyUser(String address, String username, String password)
     {
        try 
        {
            if (address != "" && username != "" && password != "") 
            {
                setDomain(UniAddress.getByName(address));
                setAuthentication(new NtlmPasswordAuthentication(null,
                        username, password));
                SmbSession.logon(getDomain(), authentication);
                return true;
            } 
            else 
            {               
                return false;
            }
        } 
        catch (UnknownHostException e) 
        {       
            return false;
        } 
        catch (SmbException e) 
        {           
            return false;
        }
    
     }// End VerifyUser Method.
     // *******************************************************************************************************
    

    Dowbload File from PC Server to Android Client using SMB Connections. where strPCPath = "smb://" + 192.168.98.23+ "/" + strFolderName + "/FileName"; blow code is download a single file includes .config extension you can used this for downloading multiple files.

    public boolean downloadConfigFileFromServer(String strPCPath , String strSdcardPath)
    {
        SmbFile smbFileToDownload = null;       
        try 
        {
            File localFilePath = new File(strSdcardPath);
    
            // create sdcard path if not exist.
            if (!localFilePath.isDirectory()) 
            {
                localFilePath.mkdir();
            }
            try 
            {                
                smbFileToDownload = new SmbFile(strPCPath , authentication);
                String smbFileName = smbFileToDownload.getName();
    
                if (smbFileName.toLowerCase().contains(".config"))
                {
                    InputStream inputStream = smbFileToDownload.getInputStream();
    
                    //only folder's path of the sdcard and append the file name after.
                    localFilePath = new File(strSdcardPath+ "/" + smbFileName);
    
                    OutputStream out = new FileOutputStream(localFilePath);
                    byte buf[] = new byte[1024];
                    int len;
                    while ((len = inputStream.read(buf)) > 0) 
                    {
                        out.write(buf, 0, len);
                    }
                    out.flush();
                    out.close();
                    inputStream.close();
                    return true;
                }
                else
                    return false;
            }// End try 
            catch (Exception e) 
            {
                e.printStackTrace();
                return false;
            }
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
            return false;
        }   
    
    }// End downloadConfigFileFromServer Method.
    // *******************************************************************************************************
    

提交回复
热议问题