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) {