java,供下载的后端代码,如何统计下载时间?

我用如下代码,可以下载文件,但是无法在后端准确统计下载时间

public ResponseEntity<Resource> downloadFixedFile(String userid,String sliceName,HttpServletRequest request) {
// 创建文件对象
            File file = new File(filePath);

            // 判断文件是否存在
            if (!file.exists()) {
                //throw new FileNotFoundException("File not found: ");// + fileName
                return ResponseEntity.notFound().build();
            }
HttpHeaders headers = new HttpHeaders();
                        headers.add("Content-Length", String.valueOf(fileSize));
                        headers.add("Content-Disposition", "attachment; filename=" +文件名);

                        // 创建文件资源对象
                        Resource resource = new FileSystemResource(file);
                        // 返回整个文件内容
                        ResponseEntity<Resource> responseEntity =  ResponseEntity.ok()
                                .headers(headers)
                                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                                .body(new InputStreamResource(resource.getInputStream()));
                        long endTime1 = System.currentTimeMillis();
                        long duration = endTime1 - startTime;
                        System.out.println("allocateModelslices,下载文件,程序耗时:" + duration + " 毫秒");
                        System.out.println(打印当前时间_精确到毫秒.打印时间到毫秒() + "      " + sliceName + "   下载文件结束");
                        // 返回整个文件内容
                        return responseEntity;

springboot项目,返回 ResponseEntity 对象的话,就无法在后端准确统计下载时间。请问这样的代码,如何修改,才能在后端准确统计下载时间?谢谢

统计不了,因为你这个只是发送到客户端的时间,但是什么时候收到,要客户端才知道。
好比你没办法在不借助收件人返回时间的情况下,单方面知道包裹花了多久送到,你程序相当于统计的只是你打包制作包裹的时间。

【相关推荐】




如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^