使用 MIME 型別“application/vnd.openxmlformats-officedocument.wordprocessingml.document”保存了“.docx”檔案。但是當我在 Spring Content 中訪問它的端點并下載它時,它不完全是 Word 檔案,而是 Zip 存檔(application/zip)。Spring Content 1.2.5 支持“.docx”檔案,我們如何修復它?
重現問題的演示專案(附上.docx檔案):https : //github.com/leonaugust/docx-problem
編輯
雖然我知道生成的檔案畢竟是 docx,我們可以選擇將其作為 Word 檔案打開,但是有沒有辦法讓客戶不那么困惑并回傳為“.docx”格式?在我的情況下,很可能會以這種格式發送大量檔案
uj5u.com熱心網友回復:
正如您spring-content-rest在類路徑上使用的那樣,@StoreRestResource我假設您正在使用它來獲取您的內容?如果情況并非如此,請告訴我,我將進行編輯。
有幾個注釋,如果物體上存在,spring 內容休息 post/put 端點將為您設定,然后由 GET 端點使用;@MimeType和@OriginalFileName。
如果您將這些注釋添加到您的物體并在您的 post 端點中適當地設定它們,則:-
@PostMapping
public UUID create(@RequestParam("file") MultipartFile multipartFile) {
File file = new File();
file.setMimeType(multipartFile.getContentType());
store.setContent(file, multipartFile.getResource());
file.setMimeType("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
file.setOriginalFileName(multipartFile.getOriginalFilename());
UUID id = repository.save(file).getId();
log.info("id {}", id);
return id;
}
當您的客戶端通過 Spring Content REST 端點獲取內容(我再次假設)時,它將設定以下標頭:
- 內容型別標題
- 內容處理表單資料附件頭
在回應上。
這兩者都將指導瀏覽器應用程式適當地處理內容。
這應該允許您從瀏覽器發出以下 get 請求。
curl -H 'Accept: application/vnd.openxmlformats-officedocument.wordprocessingml.document' http://localhost:8080/storage/b9ca6fbe-dede-4a51-b444-9e22b798e922
它應該將附件下載為 test.docx
另外,我很想知道您為什么添加自己的“創建”端點,而不是使用 Spring Data REST/Spring Content REST 端點。它會自動為您執行此操作。我認為這是因為您不想使用 Spring Data REST?
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/339653.html
