pradipta gure
pradipta gure

Reputation: 85

downloading file from Spring Rest Controller

I am trying to download a file through Spring REST Controller. Below is my code -

@RequestMapping(value="/aaa",method=RequestMethod.GET,produces=MediaType.APPLICATION_OCTATE_STREAM_VALUE) 
public ResponseEntity<byte[]> testMethod(@RequestParam("test") String test) {
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.setContentDispositionFromData("attachment","testExcel.xlsx");
    responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM);
    File file = new File("C:\\testExcel.xlsx");
    Path path = Paths.get(file.getAbsolutePath());
    ByteArrayResource resource = new ByteArrayResource(Files.readAllbytes(path));
    return new ResposeEntity<byte[]>(resource.getByteArray(),responseHeaders,HttpStatus.OK);
}

This is called in a button click. After I click the button nothing happens. While debugging this java, I could see the bytestream. In developer tools of Mozilla, I could see successful HTTP response(response in bytestream of that excel file). As per resources available on internet, browser should automatically download the file, but that's not happening.

Why downloading is not happening in browser? What's more I need to do to make it work?

NOTE : This is just for POC purpose. In actual time, I have to generate the excel file from database details, through Apache POI or some other APIs.

Upvotes: 3

Views: 12105

Answers (3)

Senthil
Senthil

Reputation: 131

Change the code like this:

public HttpEntity<byte[]> testMethod(@RequestParam("test") String test) {

    File file = new File("C:\\testExcel.xlsx");
    FileInputStream fileInputStream = new FileInputStream(file);
    byte[] array = IOUtils.toByteArray(fileInputStream);

    HttpHeaders header = new HttpHeaders();
    header.set("Content-type","application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    header.set("Content-Disposition", "attachment; filename=" + nameyouwantsaveyourfile + ".xlsx");
    header.setContentLength(array.length);
    fileInputStream.close();
    return new HttpEntity<byte[]>(array, header);
}

Upvotes: 0

E.Egiazarov
E.Egiazarov

Reputation: 931

What worked in my case was to make server send the following header:

Access-Control-Expose-Headers: Content-Disposition

So my problem had to do with CSRF issue.

Upvotes: 0

Essex Boy
Essex Boy

Reputation: 7950

The below works for me:

@RequestMapping("/")
public void index(HttpServletRequest request, HttpServletResponse response) throws IOException {

    FileInputStream inputStream = new FileInputStream(new File("C:\\work\\boot\\pom.xml"));

    response.setHeader("Content-Disposition", "attachment; filename=\"testExcel.xlsx\"");
    response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);

    ServletOutputStream outputStream = response.getOutputStream();
    IOUtils.copy(inputStream, outputStream);

    outputStream.close();
    inputStream.close();
}

Upvotes: 1

Related Questions