将InputStream写入HttpServletResponse [英] Write an InputStream to an HttpServletResponse

查看:496
本文介绍了将InputStream写入HttpServletResponse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个想要写入HttpServletResponse的InputStream。
有这种方法,由于使用了byte []

I have an InputStream that I want written to a HttpServletResponse. There's this approach, which takes too long due to the use of byte[]

InputStream is = getInputStream();
int contentLength = getContentLength();

byte[] data = new byte[contentLength];
is.read(data);

//response here is the HttpServletResponse object
response.setContentLength(contentLength);
response.write(data);

我想知道在速度和效率方面最好的方法是什么。

I was wondering what could possibly be the best way to do it, in terms of speed and efficiency.

推荐答案

只需先写入块而不是将其完全复制到Java的内存中。以下基本示例将其写入10KB的块中。这样,您最终只能获得10KB的内存使用量,而不是完整的内容长度。此外,最终用户将更快地开始获取部分内容。

Just write in blocks instead of copying it entirely into Java's memory first. The below basic example writes it in blocks of 10KB. This way you end up with a consistent memory usage of only 10KB instead of the complete content length. Also the enduser will start getting parts of the content much sooner.

response.setContentLength(getContentLength());
byte[] buffer = new byte[10240];

try (
    InputStream input = getInputStream();
    OutputStream output = response.getOutputStream();
) {
    for (int length = 0; (length = input.read(buffer)) > 0;) {
        output.write(buffer, 0, length);
    }
}






作为creme de la creme的性能,你可以使用NIO 频道 和直接分配 ByteBuffer 。在某个自定义实用程序类中创建以下实用程序/帮助程序方法,例如 Utils

public static long stream(InputStream input, OutputStream output) throws IOException {
    try (
        ReadableByteChannel inputChannel = Channels.newChannel(input);
        WritableByteChannel outputChannel = Channels.newChannel(output);
    ) {
        ByteBuffer buffer = ByteBuffer.allocateDirect(10240);
        long size = 0;

        while (inputChannel.read(buffer) != -1) {
            buffer.flip();
            size += outputChannel.write(buffer);
            buffer.clear();
        }

        return size;
    }
}

然后使用以下内容:

response.setContentLength(getContentLength());
Utils.stream(getInputStream(), response.getOutputStream());

这篇关于将InputStream写入HttpServletResponse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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