netty接收ajax上传的文件
#1、启动服务
pipeline需要添加的handler有:HttpServerCodec、HttpObjectAggregator、ChunkedWriteHandler以及自己定义的handler。
添加前面两个类之后,自定义的handler应该就能接收到FullHttpRequest了。
#2、定义自己的handler
获取HttpDataFactory,
将通道中收到的消息体转为FullHttpRequest;
获取HttpPostRequestDecoder用来解析请求。
根据decoder解析的InterfaceHttpData 获取FileUpload。
使用FileUpload对文件进行操作。
public void channelRead(ChannelHandlerContext ctx, Object httpObject) throws Exception {
if (httpObject instanceof FullHttpRequest) {
FullHttpRequest fullHttpRequest = (FullHttpRequest) httpObject;
HttpDataFactory factory =
new DefaultHttpDataFactory(DefaultHttpDataFactory.MINSIZE);
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(factory, fullHttpRequest );
try {
decoder.offer(httpContent);
} catch (ErrorDataDecoderException e) {
e.printStackTrace();
}
while (decode

本文介绍了如何利用Netty框架来接收并处理Ajax上传的文件。主要步骤包括:1) 启动服务,配置HttpServerCodec、HttpObjectAggregator和ChunkedWriteHandler等处理器;2) 定义自定义处理器,通过HttpDataFactory、HttpPostRequestDecoder解析请求,获取FileUpload对象;3) 使用FileUpload对象对上传文件进行操作,如保存到本地。前端则发送常规的Ajax请求,文件名作为参数传递。
最低0.47元/天 解锁文章
1234

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



