使用java.net.UrlConnection和outputstream从Grails 1.3.7下载大文件时出错

时间:2012-11-27 05:54:57

标签: java exception grails download broken-pipe

我们在Tomcat 1.6环境中运行Grails 1.3.7应用程序。几天前,我们的一些Pdf报告开始给我们提供问题。大小约1MB或更小的较小报告可以正常工作,但较大的报告会给我们一个'java.net.SocketException:Broken pipe'异常。

为了生成Pdf报告,我们使用itext-2.1.0。然后,我们使用java.net.URLConnection使用户能够下载生成的文件。代码如下:

// retrives file generated using itext
   def thisUrl = new File(session.getServletContext().getRealPath("/reports  /${pdffilename}")).toURI().toURL();

   def connection = null
   def pdfInputStream = null  

   try {
      connection = thisUrl.openConnection() //returns a java.net.UrlConnection
      pdfInputStream = connection.inputStream

          if (connection && pdfInputStream) {
                    connection.connectTimeout = 25* 60*1000;
                    connection.readTimeout = 25* 60*1000
                    response.setHeader "Content-disposition", "attachment; filename = ${pdffilename}"
                    response.contentType = 'pdf/pdf'
                    response.outputStream << pdfInputStream     // This line fails for large files
          } else {
                    redirect(action: 'failHandler')
         }
  } catch (e) {
         log.info('Could not report, connection may have terminated')
         throw e;
  } finally {
     response.outputStream.flush()
      response.outputStream.close()
  }

'response.outputStream&lt;&lt;对于较大的文件大小,pdfInputStream'失败,给出了以下异常:

outputstream exception

提前致谢!!

1 个答案:

答案 0 :(得分:2)

检查服务器中是否有Load Balancer。这可能会导致您的连接超时