0

i want to receive a HTTP Stream in SpringBoot but the InputStream of HttpServletRequest seems not to be an endless HTTP Stream and only contains the Content of first HTTP Body.

I want to process a chuncked HTTP Stream in SpringBoot on which is puhed some Value String from time to time.

Currently I tried something like this in a controller:

  @Override
    public void  test(HttpServletRequest request,
                      HttpServletResponse response) {
        System.out.println("StreamStart");
        try {



            byte[] buffer = new  byte[1024];
            while(true){
                int len = request.getInputStream().read(buffer);
                 if(len!=-1) {
                     System.out.println("Len: " + len);
                     System.out.println(new String(buffer));
                 }
                 Thread.sleep(500);
            }
        }
        catch(Exception x){
            x.printStackTrace();
        }

        System.out.println("StreamEnd");

    }

However the first Request Body after the header works, but the second does not appear in my Controller.

Does SpringBoot cancles the connection or the stream? Can I have access to the complete HTTP Input stream to get my values from it?

Ni9e
  • 41
  • 12

1 Answers1

0

Maybe Multipart request would be usefull for you? That way you can recieve multiple parts of data https://www.w3.org/Protocols/rfc1341/7_2_Multipart.html Example:

 @PostMapping("/upload")
public void uploadStream(@RequestParam MultipartFile[] multipartFiles){
    for(MultipartFile multipartFile:multipartFiles){
        try {
            InputStream inputStream = multipartFile.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
alexej K
  • 152
  • 3
  • nice idea, bit does multipart need a predifined content lengh? i need some streaming without a predefined content lengh because the client is pushing data to the server via HTTP Streaming and i can't know the content lengh – Ni9e Jan 03 '20 at 11:59
  • I want to relize a endless streaming between Server and Client, i don't think it's possible with multipart. – Ni9e Jan 03 '20 at 12:03
  • I used it already as streaming from client to server, here is good description how to do it. For content length you can set properties to large value. https://stackoverflow.com/questions/32782026/springboot-large-streaming-file-upload-using-apache-commons-fileupload – alexej K Jan 03 '20 at 12:08
  • Thanks but can I modify HTTP Header content len to a big number and then it will work as streaming? I cant imagine that – Ni9e Jan 03 '20 at 12:12
  • No, not that way. You need a server that reads multipart streaming and you client that send it. https://commons.apache.org/proper/commons-fileupload/apidocs/org/apache/commons/fileupload/MultipartStream.html for server part – alexej K Jan 03 '20 at 12:26
  • ok, i think that multipart streaming is for files but not for content with a undefined content len – Ni9e Jan 03 '20 at 12:40