1

サーバーからAndroidデバイスにすべてのファイルをコピーしたい。サーバーで、私のサーバーIPがhttp://192.168.98.23で、サーバーフォルダーの名前がDataであるとします。Dataフォルダには多くのファイルが含まれています。すべてのファイルをサーバーデータから Android デバイスの SD カードにコピーしたいと考えています。

これどうやってするの?

4

2 に答える 2

2

あなたが言うことができるように、LANを使用してサーバーからAndroid(SDカード)にファイルを転送しています。この目的のために、使用できる 2 つのアプローチがあります。つまり、i) TCP/IP プロトコル。ii) SMB (サーバー メッセージ ブロック) プロトコル。SMB プロトコルを使用することをお勧めします。これは、完全なアクセス許可を持つフォルダーを共有し、すべてのファイルを Android Sdcard にコピーするだけでよいためです。この場合のクライアント側である Android 側では、4 つのことを使用する必要があります。i) サーバーの IP アドレス。ii) サーバーのパスワード。iii) サーバーのユーザー名と最後の iv) 共有フォルダー名。これら 4 つのパラメーターを使用して、接続を確立し、共有フォルダーに配置されたすべてのファイルをコピーします。

smb プロトコルを使用して接続を確立するために使用されるコード スニペットに従います。

 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.
 // *******************************************************************************************************

SMB 接続を使用して、PC サーバーから Android クライアントにファイルをダウンロードします。ここで、strPCPath = "smb://" + 192.168.98.23+ "/" + strFolderName + "/FileName"; 打撃コードは、複数のファイルをダウンロードするために使用できる .config 拡張子を含む単一のファイルをダウンロードします。

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.
// *******************************************************************************************************
于 2012-10-08T10:54:19.043 に答える
1

このロジックは、サーバーからデータを .Zip ファイルとしてダウンロードします。これにより、ドメイン サーバー フォルダーからデータが取得され、PATH=""/data/data/your_pkg_name/app_my_sub_dir/images/" に保存されます。
// コンテンツのダウンロード

            Thread t = new Thread() {
                @Override
                public void run() {
                    try {


                        URL url = new URL(
                                "http://192.168.98.23/Data");
                        HttpURLConnection connection = (HttpURLConnection) url
                                .openConnection();

                        connection.connect();

                        int fileLength = connection.getContentLength();

                        //System.out.println("fileLength: " + fileLength);

                        int size, BUFFER_SIZE = 8192;
                        int total = 0, progress = 0;
                        byte[] buffer = new byte[BUFFER_SIZE];
                        String PATH = "/data/data/your_pkg_name/app_my_sub_dir/";
                        String location = PATH + "images/";
                        try {
                            if (!location.endsWith("/")) {
                                location += "/";
                            }
                            File f = new File(location);
                            if (!f.isDirectory()) {
                                f.mkdirs();
                            }
                            ZipInputStream zin = new ZipInputStream(
                                    connection.getInputStream());
                            try {
                                ZipEntry ze = null;
                                while ((ze = zin.getNextEntry()) != null) {
                                    String path = location + ze.getName();
                                    File unzipFile = new File(path);

                                    if (ze.isDirectory()) {
                                        if (!unzipFile.isDirectory()) {
                                            unzipFile.mkdirs();
                                        }
                                    } else {
                                        // check for and create parent
                                        // directories if they don't exist
                                        File parentDir = unzipFile
                                                .getParentFile();
                                        if (null != parentDir) {
                                            if (!parentDir.isDirectory()) {
                                                parentDir.mkdirs();
                                            }
                                        }

                                        // unzip the file
                                        FileOutputStream out = new FileOutputStream(
                                                unzipFile, false);
                                        BufferedOutputStream fout = new BufferedOutputStream(
                                                out, BUFFER_SIZE);
                                        try {
                                            while ((size = zin.read(buffer, 0,
                                                    BUFFER_SIZE)) != -1) {
                                                total += size;
                                                progress += total * 70 / fileLength;
                                                if (progress == 1) {
                                                    progressBarStatus = progressBarStatus
                                                            + progress;
                                                    handlerProgressBar
                                                            .sendEmptyMessage(0);
                                                    total = progress = 0;
                                                }
                                                fout.write(buffer, 0, size);
                                                fout.flush();
                                            }

                                            zin.closeEntry();
                                        } finally {
                                            fout.close();
                                        }
                                    }
                                }
                            } finally {
                                zin.close();
                            }
                        } catch (Exception e) {

                        }
                        // this.notify();
                    } catch (Exception e) {
                        interrput=true;
                        handler.sendEmptyMessage(1);
                    }
                }
            };

            t.start();
于 2013-12-06T13:13:52.957 に答える