Get opened input stream from rest template for large file processing

zibi

I am looking for a way to get opened input stream from rest template - I was trying to used ResponseExtractor, but the stream is getting closed before returning, as written here:

https://jira.spring.io/browse/SPR-7357

"Note that you cannot simply return the InputStream from the extractor, because by the time the execute method returns, the underlying connection and stream are already closed"

I hope that there is a way and I will not have to write to my output stream directly in the rest template.

zibi

I didn't find a way to do it, the stream is always getting closed. As a workaround I created the following code:

public interface ResourceReader {
    void read(InputStream content);
}

with the following implementation:

public class StreamResourceReader implements ResourceReader {

private HttpServletResponse response;

public StreamResourceReader(HttpServletResponse response) {
    this.response = response;
}

@Override
public void read(InputStream content) {
    try {
        IOUtils.copy(content, response.getOutputStream());
    } catch (IOException e) {
        throw new IllegalStateException(e);
    }
}
}

then in controller:

@RequestMapping(value = "document/{objectId}")
public void getDocumentContent(@PathVariable String objectId, HttpServletResponse response) {
    ResourceReader reader = new StreamResourceReader(response);
    service.readDocumentContent(objectId, reader);
}

call to rest template:

restTemplate.execute(uri, HttpMethod.GET, null,
            new StreamResponseExtractor(reader));

and the string response extractor:

@Override
public ResponseEntity extractData(ClientHttpResponse response) throws IOException {
    reader.read(response.getBody());
    return null;
}

and it works like a charm! :)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to get input stream from input stream file?

From Dev

how to get input stream from input stream file?

From Dev

Java Processing input from a file

From Dev

REST endpoint (GET) with large input data

From Dev

REST endpoint (GET) with large input data

From Dev

Download Large file from server using REST template Java Spring MVC

From Dev

Stream a large file from http service

From Dev

Processing: Write HTML file from arduino input

From Dev

Could not get audio input stream from input stream

From Dev

Processing large file in java

From Dev

Will Node.js get blocked when processing large file uploads?

From Dev

Will Node.js get blocked when processing large file uploads?

From Dev

How to get xml file as input stream from Url and make xml parsing

From Dev

VB.NET get path from the file the user opened

From Dev

Cannot open large concatenated zip file with unzip, though it opened fine with Archive Utility, get a central directory error

From Dev

get the class from input file

From Dev

Get list of links from large text file

From Dev

parsing and get value from large file php

From Dev

large text file processing error

From Dev

Get byte stream from an audio file?

From Dev

How to get number of properties from file stream

From Dev

Get file object from file Input

From Dev

How to get one stream from error stream and input stream when calling a script using JSCH

From Dev

How to get one stream from error stream and input stream when calling a script using JSCH

From Dev

Is there a way to get file object from the form in template?

From Dev

Android, create file from input stream hangs inside while loop

From Dev

How to stream from an input file into a conduit that has a state

From Dev

Java 8 Stream Multiple Object Creation from an input File

From Dev

Overload input stream operator>> for reading objects from file

Related Related

  1. 1

    how to get input stream from input stream file?

  2. 2

    how to get input stream from input stream file?

  3. 3

    Java Processing input from a file

  4. 4

    REST endpoint (GET) with large input data

  5. 5

    REST endpoint (GET) with large input data

  6. 6

    Download Large file from server using REST template Java Spring MVC

  7. 7

    Stream a large file from http service

  8. 8

    Processing: Write HTML file from arduino input

  9. 9

    Could not get audio input stream from input stream

  10. 10

    Processing large file in java

  11. 11

    Will Node.js get blocked when processing large file uploads?

  12. 12

    Will Node.js get blocked when processing large file uploads?

  13. 13

    How to get xml file as input stream from Url and make xml parsing

  14. 14

    VB.NET get path from the file the user opened

  15. 15

    Cannot open large concatenated zip file with unzip, though it opened fine with Archive Utility, get a central directory error

  16. 16

    get the class from input file

  17. 17

    Get list of links from large text file

  18. 18

    parsing and get value from large file php

  19. 19

    large text file processing error

  20. 20

    Get byte stream from an audio file?

  21. 21

    How to get number of properties from file stream

  22. 22

    Get file object from file Input

  23. 23

    How to get one stream from error stream and input stream when calling a script using JSCH

  24. 24

    How to get one stream from error stream and input stream when calling a script using JSCH

  25. 25

    Is there a way to get file object from the form in template?

  26. 26

    Android, create file from input stream hangs inside while loop

  27. 27

    How to stream from an input file into a conduit that has a state

  28. 28

    Java 8 Stream Multiple Object Creation from an input File

  29. 29

    Overload input stream operator>> for reading objects from file

HotTag

Archive