为什么JasperViewer只适用于localhost? [英] Why does JasperViewer only works on localhost?

查看:470
本文介绍了为什么JasperViewer只适用于localhost?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道为什么JasperViewer只适用于localhost。当我将项目部署到服务器时,客户端无法再查看报告。

I was just wondering why JasperViewer only works on localhost. When I deployed my project to server, clients can not be able to view the reports anymore.

String reportDir = getServletContext().getRealPath("WEB-INF/classes/com/proj/reports");
        String fileName = reportDir + "\\" + request.getParameter("reportName") + ".jasper";
        File outReportDir = new File("C:/REPORTS_FOLDER");
        outReportDir.mkdir();
        long millis = System.currentTimeMillis();
        String outFileName = outReportDir + "\\" + request.getParameter("reportName") + "_" + millis + ".pdf";
        HashMap parameters = new HashMap();
        parameters.put("P_BOOKING_MONTH", request.getParameter("selMonth"));
        parameters.put("P_BOOKING_YR", request.getParameter("selYear"));

        try {
            Connection conn = ConnectionUtil.getConnection();
            JasperPrint print = JasperFillManager.fillReport(fileName, parameters, conn);
            JRExporter exporter = new net.sf.jasperreports.engine.export.JRPdfExporter();
            exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, outFileName);
            exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
            exporter.exportReport();

            JasperViewer.viewReport(print, false); }...


推荐答案

JasperViewer 是< a href =https://it.wikipedia.org/wiki/Swing_(Java)\"rel =nofollow noreferrer> swing 组件,它在执行命令的计算机上启动(通常在已安装的应用程序中使用) ),因此,如果您在服务器上执行该命令,它将在服务器上打开(或抛出 HeadlessException ,如果没有配置屏幕),我们不能在服务器应用程序中使用此命令。

JasperViewer is a swing component it is launched on the computer that executes the command (normally used in installed applications), hence if you execute the command on server it will open on server (or throw a HeadlessException, if not configured to have a screen), conclusion we can't use this command in our server application.

您可以使用小程序在客户端计算机上启动命令但我强烈建议不要使用它(它在浏览器中的支持是正在减少,因此您无法确定它是否适用于所有客户端)

You could use an applet to launch the command on client computer but I strongly recommend not to use this (its support in browser is decreasing, so you can't be sure that it's working for all clients)

通常做的是将导出为pdf (html或其他选择的格式)发送到客户端浏览器,客户端可以用喜欢的程序打开文件并进行预览。

Normally what is done instead is that an export to pdf (html or other format of choice) is sent to client browser, client can open file with favorite program and preview it.

在您的示例代码中,您已经导出为pdf,将 pdf直接传递给客户端。因此删除

In your example code you are already exporting to pdf, pass the pdf directly to client. Hence remove

JasperViewer.viewReport(print, false);

这篇关于为什么JasperViewer只适用于localhost?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆