netty-http-response

时间:2015-03-18 12:28:00   收藏:0   阅读:1174

在response中可以设置cookie,可以返回非html页面,如jpg或zip等。

设置cookie

response的eader中说明了浏览器需要记住哪些cookie。见下图。
技术分享
io.netty.handler.codec.http.DefaultCookie.DefaultCookie(String name, String value)
创建一个cookie。
HttpHeaders io.netty.handler.codec.http.HttpMessage.headers()
返回Http消息的头部。
HttpHeaders io.netty.handler.codec.http.HttpHeaders.set(CharSequence name, Object value)
设置头部的键值对。
CharSequence io.netty.handler.codec.http.HttpHeaders.Names.SET_COOKIE
字符串常量,内容为"Set-Cookie"。
String io.netty.handler.codec.http.ClientCookieEncoder.encode(Cookie cookie)
cookie结构体编码为字符串,以便放入httpResponse的头部。
sample
Cookie cookie=new DefaultCookie("captchaID",captcha.getCaptchaImageID());
response.headers().set(HttpHeaders.Names.SET_COOKIE, ClientCookieEncoder.encode(cookie)); 

返回图片等其他内容

1.设置头部
response.headers().set(CONTENT_TYPE, "mage/jpeg; charset=UTF-8");//说明content为图片
2.写入二进制数据
ByteBuf io.netty.buffer.ByteBuf.writeBytes(ByteBuf src)
先将byte转为ByteBuf,再通过此方法把二进制数据写入。

原文:http://blog.csdn.net/chuchus/article/details/44407237

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!