Download service file Rest with Ajax

0

I need to download an Excel (.xlsx) file that is generated by a rest Spring service. Does anyone have an example of how to do this?

The controller rest looks something like this:

    private static final String PATH_FILE = "/home/testes/Arquivos/Products.xlsx";

@GetMapping
public void download(HttpServletRequest request, HttpServletResponse response) throws IOException {
    File file = new File(PATH_FILE);

    InputStream input = new BufferedInputStream(new FileInputStream(file));
    String mimeType = URLConnection.guessContentTypeFromStream(input);

    if(mimeType == null) {
        mimeType = "application/octet-stream";
    }

    response.setContentType(mimeType);
    response.setContentLength((int) file.length());
    response.setHeader("Content-Disposition", String.format("attachment; filename=\"%s\"", file.getName()));

    FileCopyUtils.copy(input, response.getOutputStream());
}
    
asked by anonymous 15.08.2018 / 19:09

0 answers