简体   繁体   English

如何在没有分块响应的纯Java项目中的Play框架中发送InputStream?

[英]How to send a InputStream in play framework in an java only project without chunked responses?

In a Java (only) Play 2.3 project we need to send a non-chunked response of an InputStream directly to the client. 在Java(仅)Play 2.3项目中,我们需要直接向客户端发送InputStream的非分块响应。 The InputStream comes from a remote service from which we want to stream directly to the client, without blocking or buffering to a local file. InputStream来自远程服务,我们希望从该服务直接将其流传输到客户端,而不会阻塞或缓冲到本地文件。 Since we know the size before reading the input stream, we do not want a chunked response. 因为我们在读取输入流之前就知道了大小,所以我们不需要分块的响应。

What is the best way to return a result for an input stream with a known size? 对于已知大小的输入流,返回结果的最佳方法是什么? (preferable without using Scala). (最好不使用Scala)。

When looking at the default ok(file, ..) method for returning File objects it goes deep into play internals which are only accessible from scala, and it uses the play-internal execution context which can't even be accessed from outside. 当查看默认的ok(file, ..)方法以返回File对象时,它会进入只能从scala访问的播放内部,并且使用甚至无法从外部访问的内部播放执行上下文。 Would be nice if it would work identical, just with an InputStream . 如果它可以与InputStream一样工作,那就太好了。

FWIW I have now found a way to serve an InputStream, which basically duplicates the logic which the Results.ok(File) method to allow directly passing in an InputStream . FWIW,我现在找到了一种服务InputStream的方法,该方法基本上重复了Results.ok(File)方法允许直接传递到InputStream的逻辑。

The key is to use the scala call to create an Enumerator from an InputStream: play.api.libs.iteratee.Enumerator$.MODULE$.fromStream 关键是使用scala调用从InputStream创建一个枚举数: play.api.libs.iteratee.Enumerator$.MODULE$.fromStream

private final MessageDispatcher fileServeContext = Akka.system().dispatchers().lookup("file-serve-context");

protected void serveInputStream(InputStream inputStream, String fileName, long contentLength) {
    response().setHeader(
            HttpHeaders.CONTENT_DISPOSITION,
            "attachment; filename=\"" + fileName + "\"");

    // Set Content-Type header based on file extension.
    scala.Option<String> contentType = MimeTypes.forFileName(fileName);
    if (contentType.isDefined()) {
        response().setHeader(CONTENT_TYPE, contentType.get());
    } else {
        response().setHeader(CONTENT_TYPE, ContentType.DEFAULT_BINARY.getMimeType());
    }

    response().setHeader(CONTENT_LENGTH, Long.toString(contentLength));

    return new WrappedScalaResult(new play.api.mvc.Result(

        new ResponseHeader(StatusCode.OK, toScalaMap(response().getHeaders())),

        // Enumerator.fromStream() will also close the input stream once it is done.
        play.api.libs.iteratee.Enumerator$.MODULE$.fromStream(
            inputStream,
            FILE_SERVE_CHUNK_SIZE,
            fileServeContext),

        play.api.mvc.HttpConnection.KeepAlive()));
}

/**
 * A simple Result which wraps a scala result so we can call it from our java controllers.
 */
private static class WrappedScalaResult implements Result {

    private play.api.mvc.Result scalaResult;

    public WrappedScalaResult(play.api.mvc.Result scalaResult) {
        this.scalaResult = scalaResult;
    }

    @Override
    public play.api.mvc.Result toScala() {
        return scalaResult;
    }

}

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

相关问题 播放框架Java分块响应 - play framework Java chunked response 如何在Java Play Framework中记录请求和响应 - How to log requests and responses in Java Play Framework 播放Java 2框架,返回分块响应 - Play framework 2 Java, return chunked response 播放错误的原因不明! 框架项目并仅在项目中使用java - Unknown reason for error with Play! Framework project & use only java in project 如何添加Java项目依赖项以播放框架2.2.1 Java项目 - How to add java project dependency to play framework 2.2.1 java project Play Framework如何从本地运行和Web应用程序将conf文件夹中的文件作为Java InputStream引用 - Play Framework how to reference a file in conf folder as Java InputStream from local run and web app Gzip在游戏框架中重复响应 - Gzip chunked response in play framework 如何创建游戏! 使用JetBrains IntelliJ Idea 13的框架JAVA应用程序? 我只找到了Scala项目 - How to create Play! Framework JAVA application with JetBrains IntelliJ Idea 13? I only find the Scala project Play Framework 2 - 如何在不调用.get()的情况下等待多个WS响应? - Play Framework 2 - How can I await multiple WS responses without calling .get()? 如何将inputStream对象发送到Java EJBean? - How to send an inputStream object to Java EJBean?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM