Skip to content
Advertisement

Naming file created by ResourceResponseEntity

In my REST application I’ve got a request which is returning an excel file to a user via
return new ResourceResponseEntity(new ByteArrayResource(content) where content is byte[]

The problem

The problem is the result file is always named the same way that the request method name, which is bad because it would be hard to locate some information in that files

Question

So the question is can I somehow control the file naming process using what I am using right now, any help appreciated

Answer

You might create a HttpHeaders object with a Content-Disposition header with

HttpHeaders header = new HttpHeaders();
header.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename;

and then add this HttpHeaders object to your ResourceResponseEntity with something like .headers(header).

Advertisement