theprogrammer
theprogrammer

Reputation: 2028

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
}

Upvotes: 9

Views: 37006

Answers (4)

Thomas Fritsch
Thomas Fritsch

Reputation: 10127

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.

Upvotes: 16

sango
sango

Reputation: 161

Personally i do it with ResponseEntity something like this


@GetMapping(path = "/downloadFile")
public ResponseEntity<?> downloadFile (){

    HttpHeaders headers = new HttpHeaders();
    headers.set("Content-type", MediaType.IMAGE_JPEG_VALUE);
    headers.set("Content-Disposition","attachment; filename=\"whaterver.jpg\"")); // to view in browser change attachment to inline 

    return ResponseEntity.status(HttpStatus.OK).headers(headers).body(byteArrayFile);
}

Upvotes: 3

Mohsen
Mohsen

Reputation: 4643

As a normal way I think it's better that you create a temp file in your web container and send it as a file to client. And also you can clean your temp files base on your policy with a schedule crontab. For more information please visit save temp file and clean temp

Upvotes: 1

Antoniossss
Antoniossss

Reputation: 32535

What is wrong with this?

public byte[] download(String param) throws IOException {
    return whateverByteArrayContenHere;
}

Upvotes: 2

Related Questions