我想限制我的REST服务的下载速度,这是我的代码:泽西1.17限速
@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
您可以看看Jetty如何在其servlet中实现它:DataRateLimitedServlet。阅读他们的代码,我认为你的实现并不遥远。他们似乎使用调度程序进行写入操作,但是您已将其包装在输出中,因此应该很好 – pandaadb