netty-http-response

    xiaoxiao2026-06-11  12

    在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,再通过此方法把二进制数据写入。
    最新回复(0)