java job进度_java – 如何使用Spring Boot跟踪后端的文件下载进度?

我有一个弹簧启动应用程序,它有一个休息控制器,返回字符串和文本文件.

我想跟踪服务器端的下载进度,并将消息推送到队列.

当使用HttpServlet工作时,我只是从HttpResponse对象获得一个OutputStream并将字节推到套接字上,计算进度,如下所示:

byte[] buffer = new byte[10];

int bytesRead = -1;

double totalBytes = 0d;

double fileSize = file.length();

while ((bytesRead = inStream.read(buffer)) != -1) {

outStream.write(buffer, 0, bytesRead);

totalBytes += bytesRead;

queueClient.sendMessage(

"job: " + job + "; percent complete: " + ((int) (totalBytes / fileSize) * 100));

}

然而,有了Spring Boot,因为很多东西都发生在我的引擎盖下,我不是百分之百确定如何处理它.

我刚刚开始使用Spring Boot,所以对我很轻松!

我的控制器目前看起来像这样:

@RestController

@RequestMapping("/api/v1/")

public class MyController {

@Autowired

QueueClient queue;

@RequestMapping(value = "info/{id}", method = RequestMethod.GET)

public String get(@PathVariable long id) {

queue.sendMessage("Client requested id: " + id);

return "You requested ID: " + id;

}

@RequestMapping(value = "files/{job}", method = RequestMethod.GET)

public String getfiles(@PathVariable long job) {

queue.sendMessage("Client requested files for job: " + job);

return "You requested files for job " + job;

}

}

我需要将文件从服务器传输到客户端,而不是先将整个文件加载到内存中. Spring REST控制器可以实现这一点吗?

如何访问HttpResponse对象,还是有其他方法可以访问它?

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值