InputStream 不会关闭,或者需要很长时间才能关闭

发布于 2024-11-03 17:24:28 字数 1577 浏览 0 评论 0原文

我正在尝试将外部 mp3 下载到内部存储中。但是,我尝试下载的文件很大,因此我尝试以 1MB 的块下载它们,以便您可以在下载其余文件的同时开始播放它们。这是我的流代码:

    InputStream is = null;
    OutputStream os = null;

    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet( url );
        HttpResponse response = client.execute( get );

        MyLog.d( "Connection established" );

        byte[] buffer = new byte[8192];
        is = new BufferedInputStream( response.getEntity().getContent(), buffer.length );
        os = openFileOutput( filename, MODE_PRIVATE );

        int size;
        int totalSize = 0;

        while (( size = is.read( buffer ) ) != -1 && totalSize < 1048576) {
            os.write( buffer, 0, size );
            totalSize += size;
        }

        MyLog.d( "Finished downloading mix - " + totalSize + " bytes" );
    }
    catch (ClientProtocolException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    finally {
        if ( os != null ) {
            try {
                os.flush();
                os.close();
            }
            catch (IOException e) {
                MyLog.e( "Failed to close output stream." );
            }
        }
        if ( is != null ) {
            try {
                is.close();
            }
            catch (IOException e) {
                MyLog.e( "Failed to close input stream." );
            }
        }
    }

它可以很好地下载文件,但是当它到达finally语句中的is.close()时,它挂起。如果我等了真的很长时间,它最终就会关闭。看起来它仍在下载文件的其余部分。如何避免这种情况并立即关闭流?

I'm attempting to download an external mp3 into internal storage. However, the files I'm attempting to download are big, so I'm trying to download them in 1MB chunks so that you can begin playing them while the rest is downloaded. Here's my stream code:

    InputStream is = null;
    OutputStream os = null;

    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet get = new HttpGet( url );
        HttpResponse response = client.execute( get );

        MyLog.d( "Connection established" );

        byte[] buffer = new byte[8192];
        is = new BufferedInputStream( response.getEntity().getContent(), buffer.length );
        os = openFileOutput( filename, MODE_PRIVATE );

        int size;
        int totalSize = 0;

        while (( size = is.read( buffer ) ) != -1 && totalSize < 1048576) {
            os.write( buffer, 0, size );
            totalSize += size;
        }

        MyLog.d( "Finished downloading mix - " + totalSize + " bytes" );
    }
    catch (ClientProtocolException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
    finally {
        if ( os != null ) {
            try {
                os.flush();
                os.close();
            }
            catch (IOException e) {
                MyLog.e( "Failed to close output stream." );
            }
        }
        if ( is != null ) {
            try {
                is.close();
            }
            catch (IOException e) {
                MyLog.e( "Failed to close input stream." );
            }
        }
    }

It downloads the file fine, but when it gets to is.close() in the finally statement, it hangs. If I wait a really long time it'll eventually close. It seems like it's still downloading the rest of the file. How do I avoid this and close the stream immediately?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最近可好 2024-11-10 17:24:28

对于 HTTP,您通常仍然必须读取(并丢弃)响应流的整个其余部分 - 您不能只是关闭。必须消耗整个流。我不确定 Android 的 httpclient 是基于 commons httpclient 3 还是 4 - 使用 4,您可以使用 HttpUriRequest#abort() 提前结束。不确定 3 是否有这样的选项

编辑:查了一下,它看起来像 httpclient 3 ,你可以做 httpget.abort()

With HTTP, You normally still have to read (and throw away) the whole rest of the response stream - you cannot just close. The entire stream must be consumed. I am not sure if Android's httpclient is based on commons httpclient 3 or 4 - with 4 you can use HttpUriRequest#abort() to end early. Not sure if 3 has such an option

Edit: Looked it up and it looks like httpclient 3 , and you can do httpget.abort()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文