java web將數(shù)據(jù)導(dǎo)出為pdf格式文件代碼片段
此片段達到的效果是:訪問此請求,瀏覽器將打開新的界面并顯示pdf文件預(yù)覽,在文件預(yù)覽界面可以下載該pdf文件。
1、jsp界面代碼
<input type="button" class="btn btn-info" onclick="getVerPdf();" target="_blank" value="導(dǎo)出為pdf文件" />
2、js代碼
function getVerPdf() {
window.open('/pms/jsp/version/getPrdVerListPdf?page='
+ $("#getPage").html() + '&key=' + $("#select").val());
}
3、java代碼
/**
*
* Purpose :將產(chǎn)品版本列表導(dǎo)出為pdf格式
*
* @param req
* 請求
* @param resp
* 應(yīng)答
* @param page
* 當前頁數(shù)
*/
@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");
// 得到當前頁的數(shù)據(jù)
List<Version> verList = prdVersionSer.getAllPrdVersion(key);
if (verList.size() == 0) {
// 如果沒有數(shù)據(jù),則返回主界面并顯示提示消息
req.setAttribute("getFileMsg", "沒有符合條件的信息!");
req.setAttribute("select", key);
try {
req.getRequestDispatcher("/jsp/version/ver_list.jsp").forward(req, resp);
} catch (Exception e) {
e.printStackTrace();
}
} else {
// 如果有數(shù)據(jù),則顯示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();
}
}
}
}
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Java實現(xiàn)從數(shù)據(jù)庫導(dǎo)出大量數(shù)據(jù)記錄并保存到文件的方法
- Java數(shù)據(jù)導(dǎo)出功能之導(dǎo)出Excel文件實例
- Java實現(xiàn)Excel導(dǎo)入導(dǎo)出數(shù)據(jù)庫的方法示例
- java導(dǎo)出數(shù)據(jù)庫的全部表到excel
- Java使用poi組件導(dǎo)出Excel格式數(shù)據(jù)
- Java使用easyExcel導(dǎo)出excel數(shù)據(jù)案例
- java從mysql導(dǎo)出數(shù)據(jù)的具體實例
- java實現(xiàn)異步導(dǎo)出數(shù)據(jù)
- Java樹形結(jié)構(gòu)數(shù)據(jù)生成導(dǎo)出excel文件方法記錄
- JAVA實現(xiàn)億級千萬級數(shù)據(jù)順序?qū)С龅氖纠a
相關(guān)文章
SpringBoot2.x實現(xiàn)給Controller的RequestMapping添加統(tǒng)一前綴
這篇文章主要介紹了SpringBoot2.x實現(xiàn)給Controller的RequestMapping添加統(tǒng)一前綴,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
Spring配置數(shù)據(jù)源的三種方式(小結(jié))
本文主要介紹了Spring配置數(shù)據(jù)源的三種方式,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-01-01
使用Maven和SpringBoot搭建客戶數(shù)據(jù)清洗項目框架
這篇文章主要為大家詳細介紹了如何使用Maven和SpringBoot搭建客戶數(shù)據(jù)清洗項目框架,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2025-07-07

