最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
java web将数据导出为pdf格式文件代码片段
时间:2017-03-04 编辑:简简单单 来源:一聚教程网
此片段达到的效果是:访问此请求,浏览器将打开新的界面并显示pdf文件预览,在文件预览界面可以下载该pdf文件。
1、jsp界面代码
2、js代码
代码如下 | 复制代码 |
functiongetVerPdf() { window.open('/pms/jsp/version/getPrdVerListPdf?page=' + $("#getPage").html() +'&key='+ $("#select").val()); } |
3、java代码
代码如下 | 复制代码 |
/** * * Purpose :将产品版本列表导出为pdf格式 * * @param req * 请求 * @param resp * 应答 * @param page * 当前页数 */ @RequestMapping(value ="getPrdVerListPdf") publicvoidgetPrdTypeList(HttpServletRequest req, HttpServletResponse resp, Integer page, String key) { resp.setContentType("application/pdf"); // 弹框选择保存路径和文件名 // resp.setHeader("content-disposition", // "attachment;filename=PrdVerList.pdf");
// 得到当前页的数据 List if(verList.size() ==0) { // 如果没有数据,则返回主界面并显示提示消息 req.setAttribute("getFileMsg","没有符合条件的信息!"); req.setAttribute("select", key); try{ req.getRequestDispatcher("/jsp/version/ver_list.jsp").forward(req, resp); }catch(Exception e) { e.printStackTrace(); } }else{ // 如果有数据,则显示pdf文件 JRBeanCollectionDataSource ds =newJRBeanCollectionDataSource(verList); String reportPath =null; Map if(key !="") { map.put("prdName", verList.get(0).getPrdName()); }else{ map.put("prdName",""); } reportPath = req.getServletContext().getRealPath("/reports/prdVerListByPrdName.jasper"); InputStream is =null;
try{ is =newFileInputStream(reportPath); JasperRunManager.runReportToPdfStream(is, resp.getOutputStream(), map, ds); }catch(Exception e) { e.printStackTrace(); }finally{ if(is !=null) { try{ is.close(); }catch(IOException e) { e.printStackTrace(); } } } } } |
-
下一个: 计算两个字符串最大公有子串
相关文章
- SpringBoot测试配置属性与web启动环境解析 10-24
- vue中将el-switch值true、false改为number类型的1和0解析 10-24
- Vue中的路由配置项meta使用解读 10-24
- SpringBoot自定义bean绑定解析 10-24
- SpringBoot常用计量与bean属性校验和进制数据转换规则解析 10-24
- 工厂方法在Spring框架中的运用介绍 10-24