java - 如何在 iframe 中显示临时文件?

标签 java jsf jasper-reports

我正在使用 Java、Jasper Reports 和 JSF。我想生成 PDF 格式的报告,并可以在类似这样的 iframe 中显示它,因为在同一页面上能够将过滤器放入报告等中。即使我将路径粘贴到浏览器中,报告也会正确生成为临时文件,也会显示正确。但将该路由放入 iframe 中时出现以下错误:

Not allowed to load the local resource: file :/ / / C :/ Users / Juanes ~ 1/AppData/Local/Temp/reportePedidosTemporal1922509630584311367.pdf

这是我的代码:

public void prueba(AjaxBehaviorEvent evento)
{
    try
    {   
         Map<String, Object> parametros = new HashMap<String, Object>();
         parametros.put("autor", "Juan Esteban");
         parametros.put("titulo", "Reporte de Pedidos");

         List<PedidosVO> listaPedidos = new ArrayList<PedidosVO>();

         for (int i = 1; i <= 10; i++) 
         { 
              PedidosVO pedido = new PedidosVO(""+i,"Cliente:"+i,(i+1));
              pedido.setPuntos(i);
              listaPedidos.add(pedido); 
         }

        JasperDesign design = JRXmlLoader.load("C:\\Reportes\\Reporte2Pedidos.jrxml");

        JasperReport reporte = JasperCompileManager.compileReport(design);

        //-**-**-/*-/ 

        JasperPrint jasperPrint = JasperFillManager.fillReport(reporte, parametros,new JRBeanCollectionDataSource(listaPedidos));

        byte[] flujo  = JasperExportManager.exportReportToPdf(jasperPrint);
        File tempFile = File.createTempFile("reportePedidosTemporal",".pdf");
        System.out.println(tempFile.setReadable(true));
        escribirByte(tempFile,flujo);
        tempFile.deleteOnExit();

        if(tempFile.exists())
        {
            System.out.println("RUTA : "+tempFile.getPath());
            url = tempFile.getPath();
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }

}

任何想法、建议,我们将不胜感激

最佳答案

您需要有一个URL来指向您加载到 iframe 中的 PDF。就目前而言,您正在尝试直接从文件系统加载文件。这意味着您的站点将尝试直接从最终用户的文件系统加载文件,但由于显而易见的原因,这是不允许的。

在您的服务器上生成 PDF,然后生成指向它的 URL。然后将其设置为 iframe 源。

关于java - 如何在 iframe 中显示临时文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14202859/

相关文章:

java - 数组类型困惑?

java - 日历 MONTH 字段未更新

java - OpenCV Android Studio Java IP摄像机未连接

jasper-reports - 使用 XML 文件数据源的子报表不起作用

java - 如何从优先级队列中删除特定元素?

Java Server Faces JSF - 谁负责自动生成的 ID?

jsp - java.lang.IllegalStateException : Component javax. faces.component.UIViewRoot 不是预期的类型

jsf - p :message change text of message

jasper-reports - org.eclipse.swt.SWTException 尝试使用按钮将报告发布到 JasperReports Server

jasper-reports - 如何比较 PrintWhenExpression 中的 bool 值