我正在使用Spring Boot應用程序,並且在下載大文件時遇到問題。也就是說,當我點擊客戶端頁面中的下載鏈接時,瀏覽器下載對話框顯示需要很長時間。對於小文件,它會立即顯示,但對於大文件,最多需要30秒。無阻塞地下載大文件
這讓我覺得我的控制器阻塞,直到文件內容完全寫入響應輸出流,然後才發送到客戶端。我嘗試了各種選擇,讓它立即發送響應,但仍然沒有結果。這是我到目前爲止有:
@RequestMapping(method = RequestMethod.GET, value = "/content/file/download")
public ResponseEntity<InputStreamResource> downloadFile(@RequestParam(name = "path") String path, HttpServletRequest request)
throws IOException, NotFoundException, BadRequestException, InternalServerErrorException{
getApplicationId(request);
String orgId = getOrganizationId(request);
LOGGER.debug("Downloading for path {}", path);
try{
Session session = getCurrentSession(request);
FileStorageService service = new JCRFileStorageService(session);
ProtonItem item = service.getItem(path, orgId);
InputStream is = service.getFileDownloadStream(path, orgId);
InputStreamResource resource = new InputStreamResource(is);
HttpHeaders headers = new HttpHeaders();
headers.setContentLength(item.getMetadata().getMetadata(ProtonBaseConstants.META_FILE_SIZE, Long.class));
headers.set(HttpHeaders.CONTENT_TYPE, item.getMetadata().getMetadata(ProtonBaseConstants.META_MIME_TYPE, String.class));
headers.set(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + item.getName());
return ResponseEntity.ok().headers(headers).body(resource);
} catch(ProtonRepoException ex){
throw ProtonExceptionMapper.processException(ex);
}
}
各種信息來源的在線點,這應該是足夠了下載立即開始,但它不工作。我嘗試使用IOUtils.copy()
,直接從輸入到輸出流複製,但似乎沒有任何工作。
有什麼我失蹤了嗎?也許一些Spring Boot配置,或者其他什麼?
什麼是'service.getFileDownloadStream'正在做...讀取內存中的某個字節[]'也許?與其猜測,您可能想要將度量指標添加到您的應用程序中,以找出需要很長時間的問題。 –
不,這個方法只是將'InputStream'返回到底層文件。我確實檢查過了,這不是問題,只需要幾個毫秒即可完成。 – vdjurovic
你如何創建大文件?例如SQL結果集映射到對象,然後映射到文件,如果是這種情況,則直接跳過結果集中的映射和流。 – Ibrahim