java web将数据导出为pdf格式文件代码片段
程序员文章站
2024-03-07 17:20:39
此片段达到的效果是:访问此请求,浏览器将打开新的界面并显示pdf文件预览,在文件预览界面可以下载该pdf文件。
1、jsp界面代码
此片段达到的效果是:访问此请求,浏览器将打开新的界面并显示pdf文件预览,在文件预览界面可以下载该pdf文件。
1、jsp界面代码
<input type="button" class="btn btn-info" onclick="getverpdf();" target="_blank" value="导出为pdf文件" />
2、js代码
function getverpdf() { 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") public void getprdtypelist(httpservletrequest req, httpservletresponse resp, integer page, string key) { resp.setcontenttype("application/pdf"); // 弹框选择保存路径和文件名 // resp.setheader("content-disposition", // "attachment;filename=prdverlist.pdf"); // 得到当前页的数据 list<version> verlist = prdversionser.getallprdversion(key); 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 = new jrbeancollectiondatasource(verlist); string reportpath = null; map<string, object> map = new hashmap<string, object>(); 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 = new fileinputstream(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(); } } } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
上一篇: PHP进程通信基础之信号