Skip to content

Instantly share code, notes, and snippets.

@limingzju
Created May 9, 2015 03:49
Show Gist options
  • Save limingzju/6685cae8f7d1333ac9a0 to your computer and use it in GitHub Desktop.
Save limingzju/6685cae8f7d1333ac9a0 to your computer and use it in GitHub Desktop.
spring return binary file
@RequestMapping(value="/pdfmethod", produces="application/pdf")
public void pdfMethod(HttpServletRequest request, HttpServletResponse response){
response.setContentType("application/pdf");
InputStream inputStream = null;
OutputStream outputStream = null;
try{
inputStream = getInputStreamFromYourPdfFile();
outputStream = response.getOutputStream();
IOUtils.copy(inputStream, outputStream);
}catch(IOException ioException){
//Do something or propagate up..
}finally{
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outputStream);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment