Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return byte array as an in memory file in java spring boot?

I have a byte array as data. Now how do I write a controller method to return this byte array as file using Spring Boot? If I create a file out of this byte array data, then I should also take care of deleting it right?

Is there a way to send this byte array as file without having to physically create a file in my project, maybe send all bytes through the network or something?

However if that's not possible, is file creation, responding in rest api and then deleting it is the only way to solve this? My controller method would look like this in spring boot

@GetMapping("/download")
public ResponseEntity<Resource> download(String param) throws IOException {
    // Assume I already have this byte array from db or something
    Byte[] a = getItFromDB();

    // return it as a file without explicitly creating another file in my machine
    // I am ok with changing return type of this method from ResponseEntity to anything else if you have a solution
}
like image 403
theprogrammer Avatar asked Mar 23 '26 06:03

theprogrammer


1 Answers

Just take the byte[] array, wrap it into a ByteArrayResource (which is an implementation of the Resource interface), build a ResponseEntity<Resource> from that, and return it.

@GetMapping("/download")
public ResponseEntity<Resource> download(String param) throws IOException {
    // Assume I already have this byte array from db or something
    byte[] array = getItFromDB();

    ByteArrayResource resource = new ByteArrayResource(array);
    return ResponseEntity.ok()
            .contentType(MediaType.APPLICATION_OCTET_STREAM)
            .contentLength(resource.contentLength())
            .header(HttpHeaders.CONTENT_DISPOSITION,
                    ContentDisposition.attachment()
                        .filename("whatever")
                        .build().toString())
            .body(resource);
}

There is no need to create a temporary file, and no need to change the return type.

The media-type in the Content-Type header and the file-name in the Content-Disposition header are important hints for the web-browser (or whatever client) receiving your download. You should probably use better values than those in the code above. For example: for PNG-image content you would use MediaType.IMAGE_PNG and "whatever.png". Then the web-browser will probably open the system's favorite image viewer.

like image 153
Thomas Fritsch Avatar answered Mar 25 '26 21:03

Thomas Fritsch



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!