Controller返回PDF格式的文件的Response写法
创建一个Controller
@GetMapping("/download")
public void download(HttpServletResponse response)
{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// 把需要输出的文件写入到流中
this.write(byteArrayOutputStream )
URLEncoder encode = URLEncoder.createQuery();
response.setContentType("application/pdf;charset=utf-8");
response.setHeader("Content-Disposition", "attachment;filename=" + encode.encode("文件名称", Charset.forName("UTF-8")) + ".pdf");
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
try {
byteArrayOutputStream.writeTo(response.getOutputStream());
byteArrayOutputStream.flush();
byteArrayOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
完成