// 下载PDF
@RequestMapping(value = "")//映射路径
public void downloadPDF(HttpServletResponse response) {
//pdf所在路径
String pdffilePath = xxx;
//pdf文件名
String fileName = xxx;
// 读到流中
InputStream inStream = null;
try {
File file =new File(pdffilePath);
inStream = new FileInputStream(file);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
// 设置输出的格式
response.reset();
response.setContentType("bin");
try {
response.addHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes("gb2312"), "ISO8859_1") + "\"");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
// 循环取出流中的数据
byte[] b = new byte[10240];
int len;
try {
while ((len = inStream.read(b)) > 0)
response.getOutputStream().write(b, 0, len);
inStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
根据已有pdf下载
最新推荐文章于 2023-01-04 11:39:36 发布