浏览 3312 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (1) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2009-06-01
例子如下 import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.io.StringWriter; import java.util.HashMap; import javax.servlet.ServletException; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.jasperreports.engine.JREmptyDataSource; import net.sf.jasperreports.engine.JRException; import net.sf.jasperreports.engine.JasperRunManager; public class JasperReportServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获得输出流 ServletOutputStream outputStream = response.getOutputStream(); //获得输入流 InputStream inputStream = getServletConfig().getServletContext().getResourceAsStream("JasperFile//JasperReportFile.jasper"); try { //生成网页的PDF文件 JasperRunManager.runReportToPdfStream(inputStream, outputStream, new HashMap(), new JREmptyDataSource()); //设置PDF格式 response.setContentType("application/pdf"); outputStream.flush(); outputStream.close(); } catch (JRException e) { StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); e.printStackTrace(printWriter); response.setContentType("text/plain"); response.getOutputStream().print(stringWriter.toString()); } } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2009-07-20
很奇怪,在ff中可以正常生成pdf,在ie6则在网页上显示乱麻。
|
|
返回顶楼 | |
发表时间:2010-03-12
不太详细啊,怎么在web容器里部署啊,
|
|
返回顶楼 | |