netty 对 http 的实现

netty的http协议栈无论是性能还是可靠性,都表现优异,非常适合在非web容器场景 下应用,相比于tomcat、jetty等web容器,它更轻量、小巧、灵活性和定制性也好;

总结:只要实现了http协议规范,就可以支持http服务端开发,理解时不应该拘泥于它是容器,还不是容器。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用Netty实现HTTP下载到浏览器,你需要编写一个HTTP服务器并在其中实现文件下载的逻辑。以下是一个基本的示例代码,可以帮助你开始: ```java import io.netty.bootstrap.ServerBootstrap; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundHandlerAdapter; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.handler.codec.http.DefaultHttpResponse; import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpHeaderValues; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpResponseEncoder; import io.netty.handler.codec.http.HttpServerCodec; import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpVersion; import io.netty.handler.stream.ChunkedFile; import io.netty.handler.stream.ChunkedWriteHandler; import java.io.File; import java.io.RandomAccessFile; public class HttpServer { private static final String FILE_PATH = "/path/to/your/file"; private static final String FILE_NAME = "file.txt"; private static final int PORT = 8080; public static void main(String[] args) throws Exception { EventLoopGroup bossGroup = new NioEventLoopGroup(1); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup) .channel(NioServerSocketChannel.class) .childHandler(new ChannelInitializer<SocketChannel>() { @Override public void initChannel(SocketChannel ch) throws Exception { ChannelPipeline p = ch.pipeline(); p.addLast(new HttpServerCodec()); p.addLast(new HttpObjectAggregator(512 * 1024)); p.addLast(new ChunkedWriteHandler()); p.addLast(new HttpServerHandler()); } }); ChannelFuture f = b.bind(PORT).sync(); System.out.println("HTTP server started on port " + PORT); f.channel().closeFuture().sync(); } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } static class HttpServerHandler extends ChannelInboundHandlerAdapter { @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (msg instanceof io.netty.handler.codec.http.HttpRequest) { io.netty.handler.codec.http.HttpRequest req = (io.netty.handler.codec.http.HttpRequest) msg; if (req.method() == HttpMethod.GET) { RandomAccessFile raf = new RandomAccessFile(new File(FILE_PATH + FILE_NAME), "r"); long fileLength = raf.length(); DefaultHttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.headers().set(HttpHeaderNames.CONTENT_TYPE, "application/octet-stream"); response.headers().set(HttpHeaderNames.CONTENT_DISPOSITION, "attachment; filename=\"" + FILE_NAME + "\""); response.headers().set(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(fileLength)); response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.KEEP_ALIVE); ctx.write(response); ChannelFuture sendFileFuture = ctx.write(new ChunkedFile(raf.getChannel()), ctx.newProgressivePromise()); sendFileFuture.addListener(new ChannelFutureListener() { @Override public void operationComplete(ChannelFuture future) throws Exception { raf.close(); } }); ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE); } else { ctx.writeAndFlush(new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.METHOD_NOT_ALLOWED)) .addListener(ChannelFutureListener.CLOSE); } } } @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); ctx.close(); } } } ``` 在这个示例中,我们创建了一个HTTP服务器,监听8080端口。当收到GET请求时,我们使用RandomAccessFile读取文件并将其发送给浏览器。注意,我们使用ChunkedFile和ChunkedWriteHandler来处理大文件的传输。 你可以将FILE_PATH和FILE_NAME替换为你自己的文件路径和文件名。启动服务器后,打开浏览器,输入http://localhost:8080/,应该会下载文件到你的本地计算机。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值