2013-07-05 48 views
1

这里服务于请求的结果的PDF是从一个服务器获取PDF和在servlet的服务于它作为响应的代码:在空响应

GAELogger.logInfo(LOG, "download request. url: " + downloadURLStr); 

HTTPResponse fetchResponse = URLFetchServiceFactory.getURLFetchService().fetch(new URL(downloadURLStr)); 

byte[] pdfContent = fetchResponse.getContent(); 
Integer totalLength = pdfContent.length; 
String fileName = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date()); 

GAELogger.logInfo(LOG, "writing. total: " + totalLength.toString()); 

response.reset(); 
response.setStatus(HttpServletResponse.SC_OK); 
response.setContentType("application/pdf"); 
response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".pdf\""); 
response.setHeader("Content-Length", totalLength.toString()); 

ServletOutputStream outputStream = response.getOutputStream(); 
outputStream.write(pdfContent); 
outputStream.flush(); 
outputStream.close(); 

GAELogger.logInfo(LOG, "done."); 

这里是日志请求的服务PDF:

download request. url: http://someurl/test.pdf 
writing. total: 43497 
logInfo: done. 

,有时响应是空的和铬给出这样的错误:

enter image description here

虽然有时它确实服务于请求并下载了pdf。我已经尝试了多种方案,例如:

String downloadURLStr = outputFiles.get(0).getUrl(); 
URL downloadURL = new URL(downloadURLStr); 
URLConnection connection = downloadURL.openConnection(); 

InputStream inputStream = downloadURL.openStream(); 
OutputStream outputStream = response.getOutputStream(); 
Integer totalLength = connection.getContentLength(); 

response.setContentType("application/pdf"); 
response.addHeader("Content-Disposition", "inline; filename=\"" + id + ".pdf\""); 
response.setHeader("Content-Length", totalLength.toString()); 

IOUtils.copy(inputStream, outputStream); 

IOUtils.closeQuietly(inputStream); 
IOUtils.closeQuietly(outputStream); 

这也失败了。任何想法为什么?

回答

0

我没有很长一段时间做谷歌App Engine的东西,但当时(也许2年前)的GAE关闭线程,如果他们花了太长时间。也许你应该仔细检查GAE的限制,如果它们导致这种奇怪的行为。然而,这将回答为什么它有时工作,有时不工作。

+0

问题是,当开始出现问题就一直失败。并开始服务时,它做它一贯:(和日志告诉它所做的一切,所以我不知道WCH限制将是导致问题的2013年7月5日03:42:01.423 - 2013年7月5日03:42: 05.979是请求时间和文件大小是4.5KB。服务器是完全空闲的,所以我不明白我可能遇到的其他限制。 – Srinivas