我有一个弹簧启动应用程序,它有一个休息控制器,返回字符串和文本文件.
我想跟踪服务器端的下载进度,并将消息推送到队列.
当使用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对象,还是有其他方法可以访问它?