《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
return params;
}
public void setParams(JSONObject params) {
this.params = params;
}
}
定义了一个http封装的对象。保存对应的传参数。
2、FileServer
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import java.net.InetSocketAddress;
/**
-
作者:DarkKIng
-
创建日期:2019/12/17
-
类说明:文件下载服务端
*/
public class FileServer {
private final int port;
public FileServer(int port) {
this.port = port;
}
public static void main(String[] args) throws InterruptedException {
int port = 9999;
FileServer fileServer = new FileServer(port);
System.out.println(“服务器即将启动”);
fileServer.start();
System.out.println(“服务器关闭”);
}
public void start() throws InterruptedException {
final FileServerHandle serverHandler = new FileServerHandle();
/线程组/
EventLoopGroup group = new NioEventLoopGroup();
Pipeline pipeline = new Pipeline();
try {
/服务端启动必须/
ServerBootstrap b = new ServerBootstrap();
b.group(group)/将线程组传入/
.channel(NioServerSocketChannel.class)/指定使用NIO进行网络传输/
.localAddress(new InetSocketAddress(port))/指定服务器监听端口/
/*服务端每接收到一个连接请求,就会新启一个socket通信,也就是channel,
所以下面这段代码的作用就是为这个子channel增加handle*/
.childHandler(pipeline);
ChannelFuture f = b.bind().sync();/异步绑定到服务器,sync()会阻塞直到完成/
System.out.println("Netty server start,port is " + port);
f.channel().closeFuture().sync();/阻塞直到服务器的channel关闭/
} finally {
group.shutdownGracefully().sync();/优雅关闭线程组/<
本文详细介绍了如何使用Netty实现文件的上传和下载。通过`FileServer`、`Pipeline`和`FileServerHandle`类的代码示例,展示了如何处理HTTP请求、解码、编码以及文件读写操作。同时,还提供了处理文件上传的逻辑,包括读取文件内容和参数信息。
最低0.47元/天 解锁文章

1304

被折叠的 条评论
为什么被折叠?



