Copy all files from server to Android Device

后端 未结 2 1334
北恋
北恋 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 02:37

    This logic download a data from server as .Zip file. This will fetch data from your domain server folder and saved into the PATH=""/data/data/your_pkg_name/app_my_sub_dir/images/";
    // Download Contents

                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();
    

提交回复
热议问题