【发布时间】:2017-07-01 08:47:46
【问题描述】:
我有一个带有按钮的 jsp 页面,该链接在 servlet 上,这会创建一个 pdf 文件作为响应的流。
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
path = request.getServletContext().getRealPath("/");
String pdfFileName = "foo.pdf";
String contextPath = getServletContext().getRealPath(File.separator);
File pdfFile = new File(path + pdfFileName);
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "attachment; filename=" + pdfFileName);
response.setContentLength((int) pdfFile.length());
FileInputStream fileInputStream = new FileInputStream(pdfFile);
OutputStream responseOutputStream = response.getOutputStream();
int bytes;
while ((bytes = fileInputStream.read()) != -1) {
responseOutputStream.write(bytes);
}
}
jquery 是
$(document).ready(function() {
$(".getpdfbutton").click(function(){
var currentRow=$(this).closest("tr");
var col1=currentRow.find("td:eq(0)").html();
var data3=col1;
alert(data3);
$.get("PDFerzeugen",{Spass:data3}, function(data) {
/* window.location = data; */
alert(data);
});
});
我得到的数据响应为 base64,我如何将其下载为 pdf 文件?
【问题讨论】:
-
您的问题是什么?您的 pdf 没有显示或者您想下载它?
-
我解决了这个问题:
标签: java jquery jsp pdf servlets