java web將數(shù)據(jù)導出為pdf格式文件代碼片段
更新時間:2020年11月25日 15:20:11 作者:夏詩鳶
這篇文章主要為大家詳細介紹了java web將數(shù)據(jù)導出為pdf格式文件代碼片段,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
此片段達到的效果是:訪問此請求,瀏覽器將打開新的界面并顯示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 :將產(chǎn)品版本列表導出為pdf格式
*
* @param req
* 請求
* @param resp
* 應答
* @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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Java實現(xiàn)從數(shù)據(jù)庫導出大量數(shù)據(jù)記錄并保存到文件的方法
- Java數(shù)據(jù)導出功能之導出Excel文件實例
- Java實現(xiàn)Excel導入導出數(shù)據(jù)庫的方法示例
- java導出數(shù)據(jù)庫的全部表到excel
- Java使用poi組件導出Excel格式數(shù)據(jù)
- Java使用easyExcel導出excel數(shù)據(jù)案例
- java從mysql導出數(shù)據(jù)的具體實例
- java實現(xiàn)異步導出數(shù)據(jù)
- Java樹形結構數(shù)據(jù)生成導出excel文件方法記錄
- JAVA實現(xiàn)億級千萬級數(shù)據(jù)順序?qū)С龅氖纠a
相關文章
SpringBoot2.x實現(xiàn)給Controller的RequestMapping添加統(tǒng)一前綴
這篇文章主要介紹了SpringBoot2.x實現(xiàn)給Controller的RequestMapping添加統(tǒng)一前綴,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-02-02
使用Maven和SpringBoot搭建客戶數(shù)據(jù)清洗項目框架
這篇文章主要為大家詳細介紹了如何使用Maven和SpringBoot搭建客戶數(shù)據(jù)清洗項目框架,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下2025-07-07

