将所有文件从服务器复制到Android设备

将所有文件从服务器复制到Android设备,android,Android,我想将所有文件从服务器复制到Android设备。 假设在服务器上,我的服务器ip为http://192.168.98.23并且服务器文件夹的名称为数据。数据文件夹包含许多文件。 我想将所有文件从服务器数据复制到Android设备的SD卡 我该怎么做?正如您所说,您正在使用局域网将文件从服务器传输到Android(SD卡)。 为此,您可以使用两种方法,即TCP/IP协议。ii)SMB(服务器消息块)协议。 我建议您使用SMB协议,因为在这种情况下,您只需共享一个具有完全权限的文件夹,并将所有文件复

我想将所有文件从服务器复制到Android设备。 假设在服务器上,我的服务器ip为http://192.168.98.23并且服务器文件夹的名称为数据。数据文件夹包含许多文件。 我想将所有文件从服务器数据复制到Android设备的SD卡


我该怎么做?

正如您所说,您正在使用局域网将文件从服务器传输到Android(SD卡)。 为此,您可以使用两种方法,即TCP/IP协议。ii)SMB(服务器消息块)协议。 我建议您使用SMB协议,因为在这种情况下,您只需共享一个具有完全权限的文件夹,并将所有文件复制到Android SD卡。在Android端,在本例中,即客户端,您必须使用四种功能。i) 服务器的IP地址。ii)服务器的密码。iii)服务器的用户名和最后iv)共享文件夹名。 在这四个参数的帮助下,您可以建立连接并复制放置在共享文件夹中的所有文件

遵循用于使用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”;blow代码是下载一个包含.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.
// *******************************************************************************************************

此逻辑以.Zip文件的形式从服务器下载数据。这将从域服务器文件夹获取数据并保存到路径=“”/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();

您打算在lan或非本地网络环境中使用此任务?您打算重复此任务还是只执行一次事件?它是某个特定的服务器(例如Apache、IIS?)。您可以将文件夹转换为.zip文件,并将其下载到SD卡或应用程序路径中…是的,我想从LAN中执行此操作。因此,如何复制所有文件?如果您想从服务器下载所有文件,您必须将文件夹转换为zip文件,然后您可以下载并在路径中解压缩。我尝试了simple@Imran当前位置我错过了一些东西?如何复制?使用SMB方法,您必须使用“jcifs-1.1.11.jar”文件,因为所有SMB类都依赖于此jar文件。您可以下载它,但我没有此jar文件的链接。您可以从google搜索它。