2015-11-23 33 views
0

我需要为我在AngularJS中通过Meteor创建的PDF创建PDF。我使用Java REST服务器代理Meteor。当我在本地执行REST服务器和客户端时,它工作正常:我使用正确的名称和正确的内容下载PDF。为了生成PDF,我调用了远程Meteor(我的生产环境中的那个)。使用Java代理下载Meteor的PDF

然而,当我部署我的春节,启动应用程序,我得到一个怪异的行为:我下载使用正确的名称的PDF,但PDF内容实际上是HTML:

<!DOCTYPE html> 
<html> 
<head> 
    <link rel="stylesheet" type="text/css" class="__meteor-css__" href="/004b317ac1d5291ab47b558b4735378956c08e9b.css?meteor_css_resource=true">  
<script type="text/javascript">__meteor_runtime_config__ = JSON.parse(decodeURIComponent("%7B%22meteorRelease%22%3A%22METEOR%401.2.1%22%2C%22PUBLIC_SETTINGS%22%3A%7B%22settings_loaded%22%3Atrue%7D%2C%22ROOT_URL%22%3A%22http%3A%2F%2F100.100.100.87%3A3000%2F%22%2C%22ROOT_URL_PATH_PREFIX%22%3A%22%22%2C%22autoupdateVersion%22%3A%2243f34f41b93ffc5a984b14127100e2a44d37dda1%22%2C%22autoupdateVersionRefreshable%22%3A%226ca0239ca5c7c0b94fa69f3f0d3cd4bb0ad9b8e6%22%2C%22autoupdateVersionCordova%22%3A%22none%22%7D"));</script> 
    <script type="text/javascript" src="/8e08c879e7f7d3d9de60bbfd488162da99419f40.js?meteor_js_resource=true"></script> 
</head> 
<body> 
</body> 
</html> 

这是我的Java代码下载PDF流到客户端:

@RequestMapping(value = "get/pdf/{id}", method = RequestMethod.GET, headers = "Accept=application/pdf") 
    @Override 
    public ResponseEntity<byte[]> getPdfContrattoById(@PathVariable("id") Long id) { 

    String urlStr = buildPdfUrlFromIdContratto(id); // build the URL correctly 

    URL url = null; 
    try { 
     url = new URL(urlStr); 
    } catch (MalformedURLException e) { 
     e.printStackTrace(); 
     return null; 
    } 

    InputStream pdfFile = null; 
    try { 
     pdfFile = url.openStream(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     throw new BatException(e.getMessage()); 
    } 

    ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 

    int nRead; 
    byte[] data = new byte[2048]; 

    try { 
     while ((nRead = pdfFile.read(data, 0, data.length)) != -1) { 
      buffer.write(data, 0, nRead); 
     } 
     buffer.flush(); 
    } catch (IOException e) { 
     throw new BatException("Errore nella generazione del PDF: " + e.getMessage()); 
    } 

    byte[] bytes = buffer.toByteArray(); 

    HttpHeaders responseHeaders = new HttpHeaders(); 
responseHeaders.setContentType(MediaType.valueOf("application/pdf")); 
    responseHeaders.setContentDispositionFormData("attachment", "File " + id.toString() + ".pdf"); 

    ResponseEntity<byte[]> re = ResponseEntity 
      .ok() 
      .headers(responseHeaders) 
      .contentType(
        MediaType.parseMediaType("application/pdf")) 
      .body(bytes); 
    return re; 
} 

我在哪里可以找到问题?

回答

0

我最近做了这个。它为我工作。 替换

responseHeaders.setContentType(MediaType.valueOf("application/pdf")); 
responseHeaders.setContentDispositionFormData("attachment", "File " + id.toString() + ".pdf"); 

随着

response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); 
response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\""); 
OutputStreamWriter osw = new OutputStreamWriter(response.getOutputStream()); 
osw.write(yourPDFFileContents); 
osw.flush(); 
osw.close(); 
response.flushBuffer(); 

注意响应的类型的HttpServletResponse的。

+0

谢谢。我试图更换媒体类型;另一个头(ContentDisposition)应该是相同的。没有用你的HttpServletResponse替换我的ResponseEntity,没有任何改变。 – Manu

+0

您的请求应包含回复。 EG:public void getPdfContrattoById(@PathVariable(“id”)Long id,HttpServletResponse response)然后使用该响应将其发回。 – StackFlowed