下载需要太多时间

时间:2013-08-20 07:16:56

标签: android networking

我正在尝试在我的应用中下载文件,但下载时间不一致太长。 有时它只是在正常时间下载它,但有时它只会停留30秒或更长时间,直到它因失超错误而失败。

为什么会这样?

    private void Download(String url, String destFileName) throws IOException{
        //TODO remove that
//      File file = new File(destFileName);
//      if(file.exists())
//          return;

        if(BuildConfig.DEBUG)
            Log.d("DownloadFile", "Downloading url: " + url + ", dest: " + destFileName);

        HttpGet httppost = null;
        AndroidHttpClient client = AndroidHttpClient.newInstance("TvinciAndroid");
        FileOutputStream fos = new FileOutputStream(destFileName);

        try {

            httppost = new HttpGet(url);
            HttpResponse res = client.execute(httppost);

            if (res.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                Header[] headers = res.getHeaders("Location");
                if(headers != null && headers.length != 0) {
                    url = headers[headers.length - 1].getValue();
                    Download(url, destFileName);
                }
            }

            HttpEntity responseEntity = res.getEntity();

            if (responseEntity != null && responseEntity.getContentLength() > 0) {
                InputStream is = AndroidHttpClient.getUngzippedContent(responseEntity);
                BufferedReader reader = new BufferedReader(new InputStreamReader(is,Charset.forName("UTF-8")));
                StringBuilder bld = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    line += "\n";
                    fos.write(line.getBytes());
                    bld.append(line);
                }
                reader.close();
                if(BuildConfig.DEBUG)
                    Log.d("file content", bld.toString());
                bld = null;
            }

        } 
        catch(IOException ex){
            throw ex;
        }
        finally {
            client.close();
            fos.close();
        }
    }

非常感谢任何帮助

1 个答案:

答案 0 :(得分:-1)

尝试将缓冲区指定为8192。

//input stream to read file - with 8k buffer
InputStream input = new BufferedInputStream(url.openStream(), 8192);

我这里有一个示例工作代码可以通过URL下载文件它与您的实现不同,但这可能会对您有所帮助。

try {

        URL url = new URL(f_url[0]);
        URLConnection conection = url.openConnection();
        // getting file length
        int lengthOfFile = conection.getContentLength();

        // input stream to read file - with 8k buffer
        InputStream input = new BufferedInputStream(url.openStream(), 8192);

        // Output stream to write file
        OutputStream output = new FileOutputStream(filePath);

        byte data[] = new byte[1024];
        long total = 0;

        pDialog.setMax(lengthOfFile);
        NOTIFICATION_ID = 1+lengthOfFile;

        while ((count = input.read(data)) != -1) {

            total += count;
            // publishing the progress....
            // After this onProgressUpdate will be called
            //publishProgress(""+(int)((total*100)/lenghtOfFile));
            publishProgress(""+(int)(total));
            notifBuilder.setProgress(lengthOfFile, (int)(total), false)
                        .setContentText("Download in progress... "+total+"/"+lengthOfFile);
            nm.notify(NOTIFICATION_ID, notifBuilder.build());

            // writing data to file
            output.write(data, 0, count);

        }

        // flushing output
        output.flush();

        // closing streams
        output.close();
        input.close();

    }  catch (IOException e) {
        e.printStackTrace();
        Log.e("Error: ", e.getMessage());
        return e.getMessage()+" download failed!";
    }

我希望这有帮助。