How to download XML file from server and save it in SD card?

后端 未结 2 545
既然无缘
既然无缘 2020-11-30 12:19

In my application i\'m trying to download XML file from server and storing it in SD card.For this i\'m using following code..

        try {
               Fi         


        
2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 12:38

    public void DownloadFiles(){
    
        try {
            URL url = new URL("http://nodeload.github.com/nexes/Android-File-Manager/zipball/master");
            URLConnection conexion = url.openConnection();
            conexion.connect();
            int lenghtOfFile = conexion.getContentLength();
            InputStream is = url.openStream();
            File testDirectory = new File(Environment.getExternalStorageDirectory() + "/Folder");
            if (!testDirectory.exists()) {
                testDirectory.mkdir();
            }
            FileOutputStream fos = new FileOutputStream(testDirectory + "/zip.zip");
            byte data[] = new byte[1024];
            int count = 0;
            long total = 0;
            int progress = 0;
            while ((count = is.read(data)) != -1) {
                total += count;
                int progress_temp = (int) total * 100 / lenghtOfFile;
                if (progress_temp % 10 == 0 && progress != progress_temp) {
                    progress = progress_temp;
                }
                fos.write(data, 0, count);
            }
            is.close();
            fos.close();
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    

提交回复
热议问题