嗨老师,这是源码,就是浏览器如果要显示中文,该怎么整呢?谢谢!
package helloTry;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpVersion;
import io.netty.util.CharsetUtil;
public class CustomHandler extends SimpleChannelInboundHandler<HttpObject> {
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
// 我们利用环境上下文,先拿到channel
Channel channel = ctx.channel();
//我们先打印出客户端的远程地址
System.out.println(channel.remoteAddress());
//我们需要把要发的内容,深度复制到缓冲区
ByteBuf content = Unpooled.copiedBuffer("老师很帅",CharsetUtil.UTF_8);
//设置一个Http的response格式
FullHttpResponse response = new DefaultFullHttpResponse(
HttpVersion.HTTP_1_1,
HttpResponseStatus.OK,
content);
//设置传递数据的类型跟长度
response.headers().set(HttpHeaderNames.CONTENT_TYPE,"text/plain");
response.headers().set(HttpHeaderNames.CONTENT_LENGTH,content.readableBytes());
ctx.writeAndFlush(response);
}
}
SpringBoot/Netty+MUI全栈开发 同时搞定后台+ Android&iOS
了解课程