1

我正在使用我的 reportService 类来生成包含我的报告的 JasperPrint 对象,然后将其发送到 Servlet 并生成 PDF。问题是这个 servlet 没有在新选项卡中打开 PDF(这就是我想要的),实际上它甚至没有提示我下载它或任何东西。

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();
    }

我的 Servlet 中的 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();    
    }
}

我究竟做错了什么?

4

2 回答 2

1

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

//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();
}
于 2012-07-16T13:21:33.070 回答
1

我已经解决了将 JasperPrint 作为 byte[] 返回到我的 flex 应用程序的问题,在 flex 中它将被视为 ByteArray(因为在我的情况下,它是由 graniteds 转换的)然后我只是调用我的 servlet 发送这个 ByteArray .

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

于 2012-07-11T17:30:10.893 回答