在Play框架中调用renderBinary()后关闭InputStream [英] Close InputStream after renderBinary() call in Play Framework

查看:140
本文介绍了在Play框架中调用renderBinary()后关闭InputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Play框架中,有一个名为renderBinary的渲染方法,可以在其中渲染InputStream.

In the Play framework there is a render method called renderBinary where it is possible to render an InputStream.

据我了解,在renderBinary()方法调用之后没有任何代码运行,那么我们该如何关闭InputStream?

It was my understanding that no code is run after the renderBinary() method call, so how do we go about closing the InputStream?

以下面的示例为例:

    FileInputStream inputStream = null;
    try{
        //create temp file
        File tempFile = File.createTempFile( "foobar", ".txt" );

        inputStream = new FileInputStream( tempFile );

        //delete temp file
        tempFile.delete();

        //download the file as held in the inputstream
        renderBinary( inputStream, foobar + ".txt" );
    }
    catch( Exception e ){
        throw e;
    }
    finally {
        if( inputStream != null )
            inputStream.close();
    }

这是关闭InputStream的合适方法还是finally永远不会被调用?

Would this be a suitable way of closing the InputStream or would finally never get called?

我知道会有一个尝试一下然后看"的答案,我已经尝试过了,当我反复快速下载大文件时我没有出现任何"内存不足"错误-但我不知道如何检查InputStream是否确实关闭.

I know there will be a 'try it and see' answer, and I have tried it and I do not get any 'out of memory' errors when I repeatedly download big files in quick sucession - but I do not know how I could go about checking whether the InputStream has actually closed.

推荐答案

您可以查看源代码 实现的类该代码,它是关闭的.我真的建议下载源并将其链接到您的项目,Play在幕后做了一些魔术",很高兴看到:)

You can check the source code of the class that implements that code, it is closed. I really recommend downloading the source and linking it to your project, Play does some "magic" behind the scenes which is good to see :)

这篇关于在Play框架中调用renderBinary()后关闭InputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆