如何在android中下载文件?

时间:2018-03-29 22:13:36

标签: android httpclient

我正在使用URLConnection将文件下载到SD卡上。这很好,但我认为HTTPClient会更好。那我该怎么做?

 URL url = new URL( aurl[0] );
            URLConnection conexion = url.openConnection();
            conexion.connect();
            int lenghtOfFile = conexion.getContentLength();
            Log.d( "ANDRO_ASYNC", "Lenght of file: " + lenghtOfFile );
            InputStream input = new BufferedInputStream( url.openStream() );
            OutputStream output = new FileOutputStream( ApkFileDirectory );
            byte data[] = new byte[1024];
            long total = 0;
            while ((count = input.read( data )) != -1) {
                total += count;
                publishProgress( "" + (int) ((total * 100) / lenghtOfFile) );
                output.write( data, 0, count );
            }
            output.flush();
            output.close();
            input.close();

0 个答案:

没有答案