/java/

Spring Boot. File response

2015-12-29 16:08:41

Display file

@RequestMapping(value = "/media/file/test", method = RequestMethod.GET)
public void getFile(HttpServletResponse response) throws IOException {

    InputStream myStream = new FileInputStream("/tmp/myfilename.png");
    response.addHeader("Content-disposition", "filename=myfilename.png");
    response.setContentType("image/png");
    IOUtils.copy(myStream, response.getOutputStream());
    response.flushBuffer();
}

Download file

@RequestMapping(value = "/media/file/test", method = RequestMethod.GET)
public void getFile(HttpServletResponse response) throws IOException {

    InputStream myStream = new FileInputStream("/tmp/myfilename.png");
    response.addHeader("Content-disposition", "attachment;filename=myfilename.png");
    response.setContentType("image/png");
    IOUtils.copy(myStream, response.getOutputStream());
    response.flushBuffer();
}