我想在我的 REST 服务中限制下载速率,这是我的代码:
@GET
@Path("/laboDownloadWithLimit")
@Produces(MediaType.APPLICATION_OCTET_STREAM)
public Response getFile()
{
final File file = new File("SampleVideo_1280x720_50mb.mp4");
final StreamingOutput streamingOutput = new StreamingOutput()
{
@Override
public void write(
final OutputStream outputStream)
throws IOException, WebApplicationException
{
final InputStream inputStream = new FileInputStream(file);
final byte[] buffer = new byte[2048];
int len;
while ((len = inputStream.read(buffer)) != -1)
{
outputStream.write(buffer, 0, len);
Thread.sleep(10);
}
}
};
return Response.ok(streamingOutput)
.type(MediaType.APPLICATION_OCTET_STREAM_TYPE)
.header("Content-Disposition", "attachment; filename=\"" + file.getName() + "\"" ).build();
}
但我认为存在解决此问题的更好方法,我应该使用什么?我正在使用 Jetty 7.4.5
最佳答案
在您执行 thread.sleep(10)
的地方,您可能会考虑使用一些更谨慎的东西,例如通用的 RateLimiter
.
关于java - Jersey 1.17 速率限制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38078396/