【发布时间】:2018-02-03 06:31:35
【问题描述】:
我创建了简单的 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");
}
}
【问题讨论】: