使用 java.net.UrlConnection 和输出流下载带有 Grails 1.3.7 的大文件时出错



我们在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 <<pdfInputStream"失败,给我们以下异常:

输出流异常

提前感谢!!

检查服务器中是否有负载均衡器。这可能会导致您的连接超时

最新更新