简体   繁体   English

Android:未从网址下载完整文件

[英]Android: Complete file not downloaded from url

In my app, I have requirement to download mp3 files from url. 在我的应用中,我需要从url下载mp3文件。 I am downloading file using Async Task. 我正在使用异步任务下载文件。 For testing purpose, I have kept file in dropbox. 为了进行测试,我将文件保留在保管箱中。 The problem is, it is not downloading complete file. 问题是,它没有下载完整的文件。 The actual file size is of 5 MB. 实际文件大小为5 MB。 But, it is downloading only 29 KB of data. 但是,它仅下载29 KB的数据。 When I checked the length of content, it showed -1. 当我检查内容的长度时,它显示为-1。 I am not getting where is the problem. 我不明白问题出在哪里。 Below, I am posting my code. 在下面,我要发布我的代码。

@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

in My Application the Below Code Works Perfectly. 在我的应用程序中,以下代码可完美运行。 you can try this Out. 您可以尝试一下。

    @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;

    }

Check your link. 检查您的链接。 When you see length of content = -1, it mean there is no length of content in header of link. 当您看到内容的长度= -1时,表示链接的标题中没有内容的长度。 It don't force under your download progress. 它不会强制您进行下载。 You only download 29KB because it download website, .html not file mp3. 您仅下载29KB,因为它下载的网站是.html,而不是mp3文件。 Yout can copy and paste your link on brower to check. 您可以将链接复制并粘贴到浏览器上进行检查。

From Android Documentation: 来自Android文档:

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

getContentLength returns: getContentLength返回:

the content length of the resource that this connection's URL references, -1 if the content length is not known, or if the content length is greater than Integer.MAX_VALUE. 此连接的URL引用的资源的内容长度,如果未知,则为-1,或者如果内容长度大于Integer.MAX_VALUE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM