qq_32929101
2017-11-29 08:23java代码实现pdf文档下载
10楼主试了半天,想用controller直接实现来着,但一直都得不到文件
@Controller
@RequestMapping("/reportDownload")
public class ReportDownloadController extends BaseController{
private static Logger logger=Logger.getLogger(ReportDownloadController.class);
@RequestMapping("/downReport")
public HttpServletResponse busReport(HttpServletRequest request,BigDecimal modelId,BigDecimal planId,HttpServletResponse response) throws IOException {
UtilReport util = new UtilReport();
String fileDownloadName = null;
String fileDisplayName = null;
try {
IsReport isReport = util.initReport(modelId, planId,request);
fileDownloadName = isReport.getRptFileName();
fileDisplayName = isReport.getRptName();
// fileDisplayName = URLEncoder.encode(fileDisplayName, "UTF-8");
fileDisplayName = new String(fileDisplayName.getBytes("UTF-8"),"ISO-8859-1");
} catch (Exception e) {
System.out.println("报告不存在");
e.printStackTrace();
}
OutputStream outp = null;
BufferedInputStream bins = null;
FileInputStream in = null;
BufferedOutputStream bous = null;
try {
in = new FileInputStream(new File(fileDownloadName));
bins = new BufferedInputStream(in);
byte[] b = new byte[bins.available()];
bins.read(b);
bins.close();
response.reset();
response.addHeader("Content-Disposition", "attachment;filename=" + fileDisplayName);
response.addHeader("Content-Length", "" + new File(fileDownloadName).length());
outp = response.getOutputStream();
bous = new BufferedOutputStream(outp);
response.setContentType("application/octet-stream");
bous.write(b);
bous.flush();
bous.close();
} catch (Exception e) {
System.out.println("文件下载失败!");
e.printStackTrace();
} finally {
if (in != null) {
in.close();
in = null;
}
if (outp != null) {
outp.close();
outp = null;
}
if (bins != null) {
bins.close();
bins = null;
}
if (bous != null) {
bous.close();
bous = null;
}
}
return response;
}
- 点赞
- 回答
- 收藏
- 复制链接分享
6条回答
为你推荐
- SpringBoot上传文件到服务器的中文路径映射为前端可访问的URL地址
- java
- 1个回答
- java实现下载 设置响应头的mimetype类型问题
- java-ee
- intellij-idea
- tomcat
- 2个回答
- 用java实现 把一个文件保存到oracle数据库表中
- hibernate
- 0个回答
- 为什么使用itext生成的PDF文件只显示一页?
- 2个回答
- 怎么获取浏览器的文件下载路径
- javascript
- 浏览器
- java
- 22个回答