0

在我的应用程序中,我需要从 url 下载 mp3 文件。我正在使用异步任务下载文件。出于测试目的,我将文件保存在 Dropbox 中。问题是,它没有下载完整的文件。实际文件大小为 5 MB。但是,它只下载了 29 KB 的数据。当我检查内容的长度时,它显示为-1。我不知道问题出在哪里。下面,我发布我的代码。

@Override
    protected Void doInBackground(String... sUrl) 
    {
        InputStream input = null;
        OutputStream output = null;
        try 
        {
            URL link = new URL(sUrl[0]);
            HttpURLConnection urlConnection = (HttpURLConnection)link.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.setDoOutput(true);
            urlConnection.connect();
            int fileLength = urlConnection.getContentLength();
            Log.v("Download file length", ""+fileLength);
            input = urlConnection.getInputStream();
            int downloadedSize = 0;
            output = new FileOutputStream(Environment.getExternalStorageDirectory()
                    .getAbsolutePath()+"/audiofolder/1.mp3.mp3");

            byte[] data = new byte[1024];
            int bufferLength = 0;

            while (((bufferLength = input.read(data)) > 0)) 
            {
                output.write(data, 0, bufferLength);
                downloadedSize += bufferLength;
                publishProgress((int)((downloadedSize/fileLength) * 100));
            }//while
            output.flush();
            output.close();
            input.close();
        }//try 
        catch (MalformedURLException e) 
        {
            e.printStackTrace();
        }//catch
        catch (IOException e) 
        {
            e.printStackTrace();
        }//catch
        return null;
    }//diInBackground
4

3 回答 3

0

检查您的链接。当您看到内容长度 = -1 时,表示链接标题中没有内容长度。它不会在您的下载进度下强制执行。你只下载 29KB 因为它下载网站,.html 不是文件 mp3。您可以将链接复制并粘贴到浏览器上进行检查。

于 2013-10-11T05:39:45.737 回答
0

在我的应用程序中,以下代码完美运行。你可以试试这个。

    @Override
    protected Void doInBackground(String... aurl) {

        try {

            URL url = new URL(aurl[0]);
            URLConnection conexion = url.openConnection();
            conexion.connect();

            File file = new File("mnt/sdcard/");
            file.mkdirs();
            File outputfile = new File(file, title + ".mp3");
            FileOutputStream fileOutput = new FileOutputStream(outputfile);
            InputStream inputStream = conexion.getInputStream();
            int totalSize = conexion.getContentLength();
            int downloadedSize = 0;
            byte[] buffer = new byte[1024];
            int bufferLength = 0;  
            while ((bufferLength = inputStream.read(buffer)) != -1) {
                downloadedSize += bufferLength;
                onProgressUpdate((int) ((downloadedSize * 100) / totalSize));
                fileOutput.write(buffer, 0, bufferLength);
            }
            fileOutput.close();
        } catch (Exception e) {
            download = true;
            e.printStackTrace();

        }
        return null;

    }
于 2012-10-03T11:40:08.250 回答
0

来自 Android 文档:

https://developer.android.com/reference/java/net/URLConnection.html#getContentLength()

getContentLength 返回:

此连接的 URL 引用的资源的内容长度,如果内容长度未知,或者内容长度大于 Integer.MAX_VALUE,则为 -1。

于 2017-09-15T16:56:10.127 回答