此处demo为下载pdf文件。
response.setContentType("application/pdf");
response.setHeader("Content-disposition", "attachment;filename=epolicy.pdf");
BufferedOutputStream outp = null;
BufferedInputStream in = null;
String filenamedownload = "";
try {
outp = new BufferedOutputStream(response.getOutputStream());
in = new BufferedInputStream( new FileInputStream(filenamedownload) );
byte[] b = new byte[1024]; int i = 0; while ((i = in.read(b)) > 0) { outp.write(b, 0, i); }
} catch (IOException e) {
log.error("下载文件出错,请稍后再试!", e);
} finally {
if(outp != null){ outp.flush(); outp.close(); } if(in != null){ in.close(); }
}