简体   繁体   English

Streaming Rest Api - 从 api 以 stream 的形式向客户端发送数据块

[英]Streaming Rest Api - Send data in chunks to the client in a stream from the restful api

I have a requirement where I have to create a streaming rest api to read a file.我有一个要求,我必须创建一个流 rest api 来读取文件。 We are building something similar to cat linux command.我们正在构建类似于cat linux 命令的东西。

So in case if the user types in ccat filename |因此,如果用户输入ccat文件名 | tail, then we have to keep the stream open and read out the content in chunks of 256 bytes and wait till the resource is closed from the client. tail,然后我们必须保持 stream 打开并以 256 字节的块读取内容,然后等待资源从客户端关闭。

I have created a POC.我已经创建了一个 POC。 The rest api code looks something like this - rest api 代码看起来像这样 -

@Produces(MediaType.APPLICATION_OCTET_STREAM)
@RequestMapping(method = RequestMethod.GET, value = "stream")
public void hello(@Context HttpServletRequest request, @Context HttpServletResponse response)
  throws InterruptedException {
String content = "This is the text content";
ServletOutputStream outputStream = null;
try {
  for(int i=0;i<100;i++){
    content = content + i;
    final byte[] bytes = content.getBytes();
    outputStream = response.getOutputStream();
    response.setContentType("application/octet-stream");
    response.setStatus(200);
    outputStream.write(bytes);
    Thread.sleep(1000l);
    outputStream.flush();
  }

  outputStream.close();
} catch (IOException e) {
  e.printStackTrace();
}

And the client code is as follows -而客户端代码如下——

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class NetClientGet {
public static void main(String[] args) {

try {

  URL url = new URL("http://localhost:6868/api/stream");
  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  conn.setRequestMethod("GET");
  conn.setRequestProperty("Accept", "application/json");
  if (conn.getResponseCode() != 200) {
    throw new RuntimeException("Failed : HTTP error code : "
        + conn);
  }
  BufferedReader br = new BufferedReader(new InputStreamReader(
      (conn.getInputStream())));
  String output;
  System.out.println("Output from Server .... \n");
  while ((output = br.readLine()) != null) {
    System.out.println(output);
  }
  conn.disconnect();
} catch (MalformedURLException e) {
  e.printStackTrace();
} catch (IOException e) {
  e.printStackTrace();
}
}
}

This is working fine, but the problem is it is not writing the chunks of bytes, rather the entire content in one go. I am calling flush() so I was expecting that it will send the chunks to the client on each call of flush() but that doesn't seem to be happening.这工作正常,但问题是它不是写入字节块,而是写入一个 go 中的全部内容。我正在调用 flush() 所以我期望它会在每次调用 flush 时将块发送到客户端() 但这似乎没有发生。 It is sending to the client after the call to the close().它在调用 close() 之后发送给客户端。 How can I fix this?我怎样才能解决这个问题?

I think it will be better to use java sockets for this use case.我认为在这个用例中使用 java sockets 会更好。

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

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