2012-07-06 53 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。

我正在尋找另一種解決方案,但它可以幫助別人。

相關問題