2012-07-06 54 views
1

我正在使用我的reportService类生成包含我的报告的JasperPrint对象,然后将它发送给Servlet并生成PDF。问题是这个servlet没有在新标签中打开PDF(这是我想要的),实际上它甚至不会提示我下载它或任何东西。使用flex + servlet + jasper在新浏览器选项卡中显示PDF的问题

Servlet的来电:从我的Servlet

try { 
     URL url = new URL("http://" + serverName + ":" + serverPort + path 
       + "/reportgenerator"); 

     HttpURLConnection connection = (HttpURLConnection) url 
       .openConnection(); 

     connection.setRequestMethod("POST"); 
     connection.setDoInput(true); 
     connection.setDoOutput(true); 
     connection.setUseCaches(false); 
     connection.setDefaultUseCaches(false); 
     connection.setRequestProperty("Content-Type", 
       "application/octet-stream"); 

     ObjectOutputStream out = new ObjectOutputStream(
       connection.getOutputStream()); 

     //This "jasperPrint" is my generated report from my service 
     out.writeObject(jasperPrint); 
     out.close(); 

     connection.getInputStream(); 

    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

我doPost方法:

@Override 
protected void doPost(HttpServletRequest request, 
     HttpServletResponse response) throws ServletException, IOException { 

    JasperPrint jasperPrint = null; 
    ObjectInputStream resultStream = null; 
    ServletOutputStream out = response.getOutputStream();  

    try { 

     resultStream = new ObjectInputStream(request.getInputStream()); 
     jasperPrint = (JasperPrint) resultStream.readObject(); 
     resultStream.close(); 

     byte[] rel = JasperExportManager.exportReportToPdf(jasperPrint);    
     out.write(rel,0, rel.length); 

     //JasperExportManager.exportReportToPdfStream(jasperPrint, out); 

     response.setContentLength(rel.length);   
     response.setContentType("application/pdf"); 
     response.setHeader("Content-Disposition", 
       "attachment; filename=\"report.pdf\""); 
     response.setHeader("Cache-Control", "no-cache");       

     System.err.println(rel.length); 

    } catch (JRException e) { 
     e.printStackTrace(); 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } finally {     
     out.flush(); 
     out.close();  
    } 
} 

我在做什么错?

+0

那么它是做什么的?它是否在浏览器窗口中显示PDF?或者它是空白的?你有错误吗? – 2012-07-06 20:43:16

+0

它什么也没做。我做了一些研究,发现响应总是发送给调用者(在这个特殊情况下是HttpURLConnection)。有人能证实这样的事情吗? – 2012-07-06 20:54:25

+0

请问您为什么不在报告服务中完成所有工作?我们处理它的方式是我们从我们的服务中返回一个字节[],然后在弹性方面我们处理打开或者提示他们保存。 – 2012-07-06 21:03:01

回答

1

假设你有你想要在应用程序的弹性端打开的文件的字节[],你应该能够将文件写入临时位置然后打开它。它看起来类似于:

//create a temp dir in the system temp directory to place all the temp files for you app. 
private static var tempDir:File=File.createTempDirectory();  

/** 
* bytes - the byte array of the pdf you want to open 
* filename - the name to use for the temp file, you may need to create some type of 
*   counter to add to the beginning of the filename so that you always get 
*   a unique name 
*/ 
public static openFile(bytes:ByteArray,filename:String):void{ 
    //create a file in the system temp directory to write the file to 
    var tempFile:File = tempDir.resolvePath(filename); 

    //create a filestream to write the byte array to the file 
    var fileStream:FileStream = new FileStream(); 
    fileStream.open(tempFile, FileMode.WRITE); 
    fileStream.writeBytes(bytes,0,bytes.length); 
    fileStream.close(); 

    //open the temp file with default application 
    tempFile.openWithDefaultApplication(); 
} 
1

我已经解决了将JasperPrint作为字节[]返回给我的Flex应用程序的问题,在flex中它将被视为ByteArray(因为它已被转换,在我的情况下为granited),然后我只是打电话给我servlet发送这个ByteArray。

我正在寻找另一种解决方案,但它可以帮助别人。

相关问题