java - Jersey 1.17 速率限制

标签 java rest jersey jetty

我想在我的 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/

相关文章:

java - 网格模拟 : how to send message among peer nodes through a router?

java - JNLP 获得许可

java - CloseableHttpClient 在 OpenShift tomcat pod 上返回 504

java - 如何从 Java 中的 REST Web 服务中的 curl 获取参数值

java - 将 JSON 从 WebResource 解析为可用的 Java 对象

java - Jtabbedpane 使用多个类

rest - 什么是 https ://i. instagram.com/api/v1

Spring REST端点和服务层分离

java - Jersey REST 扩展方法

scala - SBT 无法解决 jersey-container-grizzly2-http 2.5.1 的依赖关系