2015-12-02 132 views
0

我正在嘗試創建一個java REST服務,它將下載word文檔。文件下載但內容只是垃圾十六進制,而不是實際的Word文檔內容。我的示例代碼如下。我錯過了什麼?文件之後的&具有相同數量的字節。REST服務發送損壞的文件

@SuppressWarnings("resource") 
@RequestMapping(value = "get/testdoc", method=RequestMethod.GET, produces="application/octet-stream) 
public @ResponseBody ResponseEntity<byte[]> getTestDoc() throws Throwable{ 

    File doc = new File("C:\\temp\\file.doc"); 

    InputStream is = new FileInputStream(doc); 
    byte[] bytes = IOUtils.toByteArray(is); 

    HttpHeaders responseHeaders = new HttpHeaders(); 
    responseHeaders.setContentType(MediaType.APPLICATION_OCTET_STREAM); 
    responseHeaders.set("Content-Disposition" , "Attachment; filename=file.doc"); 
    responseHeaders.setContentLength(ProposalDoc.length()); 



    return new ResponseEntity<byte[]>(bytes, responseHeaders, HttpStatus.OK); 
} 
+2

你怎麼確定下載的文件是「垃圾十六進制」?你有沒有試圖用Word打開它?此外,指定您使用的是哪個版本的Spring會很有幫助。 – Pyranja

+0

如果不指定八位位組流和內容長度,該怎麼辦? –

+0

我用word打開了文件。使用Spring 3.1.1。這需要在下載之前進行Base64編碼嗎? –

回答

0

感謝所有幫助。我最終繞過了將文件附加到響應的Spring &,如下面的代碼所示。我懷疑衝刺是在幕後轉換字節。我看着配置ByteArrayHttpMessageConverter,但似乎沒有幫助。現在這對我來說已經足夠了。

@SuppressWarnings("resource") 
@RequestMapping(value = "get/doc", method=RequestMethod.GET, produces="application/octet-stream") 
    public HttpEntity getProposalDocs(HttpServletResponse response) throws Throwable{ 
     File doc = new File("C:\\temp\\file.doc"); 

     InputStream is = new FileInputStream(doc); 

     response.setHeader("Content-Disposition", "attachment;filename=\"test.doc\""); 
     response.setHeader("Content-Type", "application/octet-stream;"); 
     StreamUtils.copy(is ,response.getOutputStream()); 

     return new ResponseEntity(HttpStatus.OK); 
    } 
1

我覺得有兩個問題:

1. Length頭:

我我看來至少有一個很奇怪的一行:

responseHeaders.setContentLength(ProposalDoc.length()); 

我認爲應該是:

responseHeaders.setContentLength(bytes.length); 

2. @ResponseBody註釋

如果您使用的返回類型ResponseEntity<byte[]>,那麼你一定不能添加@ResponseBody

@RequestMapping(value = "get/testdoc", method=RequestMethod.GET) 
public ResponseEntity<byte[]> getTestDoc() throws Throwable{ 
    ... 
} 
+0

我更新了這一行。同樣的結果。 –

+0

@史密斯先生:看看第二個問題 – Ralph

+0

謝謝。我刪除了@ResponseBody註釋,但我有同樣的問題。 –

0

嘗試更換產生= 「應用程序/八位字節流」) 與生產= 「應用程序/ vnd.ms字」)

+0

沒有幫助........ –

+0

和下載前文件打開好嗎?位於以下位置的文件:C:\\ temp \\ file.doc? –

+0

是的。該文件在下載前打開。一些編碼問題可能? –

0

查看此代碼也適用於我。

@RequestMapping(value = "/get/doc" , method = RequestMethod.GET , 
    produces = "application/msword") 
    public ResponseEntity<InputStreamResource> getProposalDocs() throws IOException{ 
    ClassPathResource docfile = new ClassPathResource("file.doc"); 
    HttpHeaders headers 

= new HttpHeaders(); 
    headers.add("Cache-Control", "no-cache, no-store, must-revalidate"); 
      headers.add("Pragma", "no-cache"); 
      headers.add("Expires", "0"); 
     return ResponseEntity.ok() 
       .headers(headers) 
       .contentLength(docfile.contentLength()) 
       .contentType(MediaType.parseMediaType("application/msword")) 
       .body(new InputStreamResource(docfile.getInputStream())); 
    } 

EDITED:與我合作返回InputStreamResource而不是byte []的想法。 還指定內容類型爲produce =「application/octet-stream」。

這正常工作與我沒有必要繞過servlet響應..

+0

添加一些文字說明您的答案如何解決OP當前問題並幫助其他具有相同問題的人。謝謝 –