[英]How to check if file was downloaded completely via Spring Rest Api
我創建了簡單的 rest api 來從 hdfs 提供文件(文件很大,我不想在本地復制它們)。
我想記錄文件下載成功完成的信息,即讀取整個流,但我不知道如何。 我只能記錄文件下載已開始的信息。
我將不勝感激任何幫助。
@Autowired
private FileDownloadService fds;
@RequestMapping(value = GET_FILE_PATH, method = RequestMethod.GET)
@Produces(MediaType.APPLICATION_OCTET_STREAM_VALUE)
public ResponseEntity getFileStream(@RequestParam("name") String name) {
LOG.info("Processing for filename: " + name);
try {
Path p = fds.getFilePath(name);
org.apache.hadoop.fs.FSDataInputStream is = fds.getFileStream(p);
return ResponseEntity.ok().header("Content-Disposition", "attachment; filename=\"" + p.getName() + "\"'").contentLength(fds.getFileLength(p))
.contentType(MediaType.APPLICATION_OCTET_STREAM).body(new InputStreamResource(is));
} catch (Exception e) {
LOG.error(e.getLocalizedMessage(), e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Internal Server Error");
} finally {
LOG.info("File: " + name + " download started");
}
}
您可以嘗試在 InputStream 上創建一個包裝器並在流關閉時觸發一些標志( close()
)。
例如,您可以將ProxyInputStream
作為基礎:
ProxyInputStreamis = new ProxyInputStream(fds.getFileStream(p)) {
@Override
public void close() throws IOException {
super.close();
// some trigger
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.